1*40411Sbostic /*-
2*40411Sbostic  * LstRemove.c --
3*40411Sbostic  *	Remove an element from a list
4*40411Sbostic  *
5*40411Sbostic  * Copyright (c) 1988 by University of California Regents
6*40411Sbostic  *
7*40411Sbostic  * Permission to use, copy, modify, and distribute this
8*40411Sbostic  * software and its documentation for any purpose and without
9*40411Sbostic  * fee is hereby granted, provided that the above copyright
10*40411Sbostic  * notice appears in all copies.  Neither the University of California nor
11*40411Sbostic  * Adam de Boor makes any representations about the suitability of this
12*40411Sbostic  * software for any purpose.  It is provided "as is" without
13*40411Sbostic  * express or implied warranty.
14*40411Sbostic  */
15*40411Sbostic #ifndef lint
16*40411Sbostic static char *rcsid =
17*40411Sbostic "$Id: lstRemove.c,v 1.7 89/06/13 15:01:51 adam Exp $ SPRITE (Berkeley)";
18*40411Sbostic #endif lint
19*40411Sbostic 
20*40411Sbostic #include	"lstInt.h"
21*40411Sbostic 
22*40411Sbostic /*-
23*40411Sbostic  *-----------------------------------------------------------------------
24*40411Sbostic  * Lst_Remove --
25*40411Sbostic  *	Remove the given node from the given list.
26*40411Sbostic  *
27*40411Sbostic  * Results:
28*40411Sbostic  *	SUCCESS or FAILURE.
29*40411Sbostic  *
30*40411Sbostic  * Side Effects:
31*40411Sbostic  *	The list's firstPtr will be set to NilListNode if ln is the last
32*40411Sbostic  *	node on the list. firsPtr and lastPtr will be altered if ln is
33*40411Sbostic  *	either the first or last node, respectively, on the list.
34*40411Sbostic  *
35*40411Sbostic  *-----------------------------------------------------------------------
36*40411Sbostic  */
37*40411Sbostic ReturnStatus
38*40411Sbostic Lst_Remove (l, ln)
39*40411Sbostic     Lst	    	  	l;
40*40411Sbostic     LstNode	  	ln;
41*40411Sbostic {
42*40411Sbostic     register List 	list = (List) l;
43*40411Sbostic     register ListNode	lNode = (ListNode) ln;
44*40411Sbostic 
45*40411Sbostic     if (!LstValid (l) ||
46*40411Sbostic 	!LstNodeValid (ln, l)) {
47*40411Sbostic 	    return (FAILURE);
48*40411Sbostic     }
49*40411Sbostic 
50*40411Sbostic     /*
51*40411Sbostic      * unlink it from the list
52*40411Sbostic      */
53*40411Sbostic     if (lNode->nextPtr != NilListNode) {
54*40411Sbostic 	lNode->nextPtr->prevPtr = lNode->prevPtr;
55*40411Sbostic     }
56*40411Sbostic     if (lNode->prevPtr != NilListNode) {
57*40411Sbostic 	lNode->prevPtr->nextPtr = lNode->nextPtr;
58*40411Sbostic     }
59*40411Sbostic 
60*40411Sbostic     /*
61*40411Sbostic      * if either the firstPtr or lastPtr of the list point to this node,
62*40411Sbostic      * adjust them accordingly
63*40411Sbostic      */
64*40411Sbostic     if (list->firstPtr == lNode) {
65*40411Sbostic 	list->firstPtr = lNode->nextPtr;
66*40411Sbostic     }
67*40411Sbostic     if (list->lastPtr == lNode) {
68*40411Sbostic 	list->lastPtr = lNode->prevPtr;
69*40411Sbostic     }
70*40411Sbostic 
71*40411Sbostic     /*
72*40411Sbostic      * Sequential access stuff. If the node we're removing is the current
73*40411Sbostic      * node in the list, reset the current node to the previous one. If the
74*40411Sbostic      * previous one was non-existent (prevPtr == NilListNode), we set the
75*40411Sbostic      * end to be Unknown, since it is.
76*40411Sbostic      */
77*40411Sbostic     if (list->isOpen && (list->curPtr == lNode)) {
78*40411Sbostic 	list->curPtr = list->prevPtr;
79*40411Sbostic 	if (list->curPtr == NilListNode) {
80*40411Sbostic 	    list->atEnd = Unknown;
81*40411Sbostic 	}
82*40411Sbostic     }
83*40411Sbostic 
84*40411Sbostic     /*
85*40411Sbostic      * the only way firstPtr can still point to ln is if ln is the last
86*40411Sbostic      * node on the list (the list is circular, so lNode->nextptr == lNode in
87*40411Sbostic      * this case). The list is, therefore, empty and is marked as such
88*40411Sbostic      */
89*40411Sbostic     if (list->firstPtr == lNode) {
90*40411Sbostic 	list->firstPtr = NilListNode;
91*40411Sbostic     }
92*40411Sbostic 
93*40411Sbostic     /*
94*40411Sbostic      * note that the datum is unmolested. The caller must free it as
95*40411Sbostic      * necessary and as expected.
96*40411Sbostic      */
97*40411Sbostic     if (lNode->useCount == 0) {
98*40411Sbostic 	free ((Address)ln);
99*40411Sbostic     } else {
100*40411Sbostic 	lNode->flags |= LN_DELETED;
101*40411Sbostic     }
102*40411Sbostic 
103*40411Sbostic     return (SUCCESS);
104*40411Sbostic }
105*40411Sbostic 
106