1 /* 2 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Adam de Boor. 7 * 8 * Redistribution and use in source and binary forms are permitted 9 * provided that the above copyright notice and this paragraph are 10 * duplicated in all such forms and that any documentation, 11 * advertising materials, and other materials related to such 12 * distribution and use acknowledge that the software was developed 13 * by the University of California, Berkeley. The name of the 14 * University may not be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 19 */ 20 21 #ifndef lint 22 static char sccsid[] = "@(#)lstRemove.c 5.2 (Berkeley) 03/11/90"; 23 #endif /* not lint */ 24 25 /*- 26 * LstRemove.c -- 27 * Remove an element from a list 28 */ 29 30 #include "lstInt.h" 31 32 /*- 33 *----------------------------------------------------------------------- 34 * Lst_Remove -- 35 * Remove the given node from the given list. 36 * 37 * Results: 38 * SUCCESS or FAILURE. 39 * 40 * Side Effects: 41 * The list's firstPtr will be set to NilListNode if ln is the last 42 * node on the list. firsPtr and lastPtr will be altered if ln is 43 * either the first or last node, respectively, on the list. 44 * 45 *----------------------------------------------------------------------- 46 */ 47 ReturnStatus 48 Lst_Remove (l, ln) 49 Lst l; 50 LstNode ln; 51 { 52 register List list = (List) l; 53 register ListNode lNode = (ListNode) ln; 54 55 if (!LstValid (l) || 56 !LstNodeValid (ln, l)) { 57 return (FAILURE); 58 } 59 60 /* 61 * unlink it from the list 62 */ 63 if (lNode->nextPtr != NilListNode) { 64 lNode->nextPtr->prevPtr = lNode->prevPtr; 65 } 66 if (lNode->prevPtr != NilListNode) { 67 lNode->prevPtr->nextPtr = lNode->nextPtr; 68 } 69 70 /* 71 * if either the firstPtr or lastPtr of the list point to this node, 72 * adjust them accordingly 73 */ 74 if (list->firstPtr == lNode) { 75 list->firstPtr = lNode->nextPtr; 76 } 77 if (list->lastPtr == lNode) { 78 list->lastPtr = lNode->prevPtr; 79 } 80 81 /* 82 * Sequential access stuff. If the node we're removing is the current 83 * node in the list, reset the current node to the previous one. If the 84 * previous one was non-existent (prevPtr == NilListNode), we set the 85 * end to be Unknown, since it is. 86 */ 87 if (list->isOpen && (list->curPtr == lNode)) { 88 list->curPtr = list->prevPtr; 89 if (list->curPtr == NilListNode) { 90 list->atEnd = Unknown; 91 } 92 } 93 94 /* 95 * the only way firstPtr can still point to ln is if ln is the last 96 * node on the list (the list is circular, so lNode->nextptr == lNode in 97 * this case). The list is, therefore, empty and is marked as such 98 */ 99 if (list->firstPtr == lNode) { 100 list->firstPtr = NilListNode; 101 } 102 103 /* 104 * note that the datum is unmolested. The caller must free it as 105 * necessary and as expected. 106 */ 107 if (lNode->useCount == 0) { 108 free ((Address)ln); 109 } else { 110 lNode->flags |= LN_DELETED; 111 } 112 113 return (SUCCESS); 114 } 115 116