1 /*- 2 * LstAppend.c -- 3 * Add a new node with a new datum after an existing node 4 * 5 * Copyright (c) 1988 by University of California Regents 6 * 7 * Permission to use, copy, modify, and distribute this 8 * software and its documentation for any purpose and without 9 * fee is hereby granted, provided that the above copyright 10 * notice appears in all copies. Neither the University of California nor 11 * Adam de Boor makes any representations about the suitability of this 12 * software for any purpose. It is provided "as is" without 13 * express or implied warranty. 14 */ 15 #ifndef lint 16 static char *rcsid = 17 "$Id: lstAppend.c,v 1.6 89/06/13 15:01:33 adam Exp $ SPRITE (Berkeley)"; 18 #endif lint 19 20 #include "lstInt.h" 21 22 /*- 23 *----------------------------------------------------------------------- 24 * Lst_Append -- 25 * Create a new node and add it to the given list after the given node. 26 * 27 * Results: 28 * SUCCESS if all went well. 29 * 30 * Side Effects: 31 * A new ListNode is created and linked in to the List. The lastPtr 32 * field of the List will be altered if ln is the last node in the 33 * list. lastPtr and firstPtr will alter if the list was empty and 34 * ln was NILLNODE. 35 * 36 *----------------------------------------------------------------------- 37 */ 38 ReturnStatus 39 Lst_Append (l, ln, d) 40 Lst l; /* affected list */ 41 LstNode ln; /* node after which to append the datum */ 42 ClientData d; /* said datum */ 43 { 44 register List list; 45 register ListNode lNode; 46 register ListNode nLNode; 47 48 if (LstValid (l) && (ln == NILLNODE && LstIsEmpty (l))) { 49 goto ok; 50 } 51 52 if (!LstValid (l) || LstIsEmpty (l) || ! LstNodeValid (ln, l)) { 53 return (FAILURE); 54 } 55 ok: 56 57 list = (List)l; 58 lNode = (ListNode)ln; 59 60 PAlloc (nLNode, ListNode); 61 nLNode->datum = d; 62 nLNode->useCount = nLNode->flags = 0; 63 64 if (lNode == NilListNode) { 65 if (list->isCirc) { 66 nLNode->nextPtr = nLNode->prevPtr = nLNode; 67 } else { 68 nLNode->nextPtr = nLNode->prevPtr = NilListNode; 69 } 70 list->firstPtr = list->lastPtr = nLNode; 71 } else { 72 nLNode->prevPtr = lNode; 73 nLNode->nextPtr = lNode->nextPtr; 74 75 lNode->nextPtr = nLNode; 76 if (nLNode->nextPtr != NilListNode) { 77 nLNode->nextPtr->prevPtr = nLNode; 78 } 79 80 if (lNode == list->lastPtr) { 81 list->lastPtr = nLNode; 82 } 83 } 84 85 return (SUCCESS); 86 } 87 88