1 /* 2 * Copyright (c) 1988, 1989, 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Adam de Boor. 7 * 8 * %sccs.include.redist.c% 9 */ 10 11 #ifndef lint 12 static char sccsid[] = "@(#)lstInsert.c 8.2 (Berkeley) 04/28/95"; 13 #endif /* not lint */ 14 15 /*- 16 * LstInsert.c -- 17 * Insert a new datum before an old one 18 */ 19 20 #include "lstInt.h" 21 22 /*- 23 *----------------------------------------------------------------------- 24 * Lst_Insert -- 25 * Insert a new node with the given piece of data before the given 26 * node in the given list. 27 * 28 * Results: 29 * SUCCESS or FAILURE. 30 * 31 * Side Effects: 32 * the firstPtr field will be changed if ln is the first node in the 33 * list. 34 * 35 *----------------------------------------------------------------------- 36 */ 37 ReturnStatus Lst_Insert(l,ln,d)38Lst_Insert (l, ln, d) 39 Lst l; /* list to manipulate */ 40 LstNode ln; /* node before which to insert d */ 41 ClientData d; /* datum to be inserted */ 42 { 43 register ListNode nLNode; /* new lnode for d */ 44 register ListNode lNode = (ListNode)ln; 45 register List list = (List)l; 46 47 48 /* 49 * check validity of arguments 50 */ 51 if (LstValid (l) && (LstIsEmpty (l) && ln == NILLNODE)) 52 goto ok; 53 54 if (!LstValid (l) || LstIsEmpty (l) || !LstNodeValid (ln, l)) { 55 return (FAILURE); 56 } 57 58 ok: 59 PAlloc (nLNode, ListNode); 60 61 nLNode->datum = d; 62 nLNode->useCount = nLNode->flags = 0; 63 64 if (ln == NILLNODE) { 65 if (list->isCirc) { 66 nLNode->prevPtr = nLNode->nextPtr = nLNode; 67 } else { 68 nLNode->prevPtr = nLNode->nextPtr = NilListNode; 69 } 70 list->firstPtr = list->lastPtr = nLNode; 71 } else { 72 nLNode->prevPtr = lNode->prevPtr; 73 nLNode->nextPtr = lNode; 74 75 if (nLNode->prevPtr != NilListNode) { 76 nLNode->prevPtr->nextPtr = nLNode; 77 } 78 lNode->prevPtr = nLNode; 79 80 if (lNode == list->firstPtr) { 81 list->firstPtr = nLNode; 82 } 83 } 84 85 return (SUCCESS); 86 } 87 88