1 /*- 2 * LstInsert.c -- 3 * Insert a new datum before an old one 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: lstInsert.c,v 1.6 89/06/13 15:01:43 adam Exp $ SPRITE (Berkeley)"; 18 #endif lint 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 38 Lst_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