140412Sbostic /*
262087Sbostic * Copyright (c) 1988, 1989, 1990, 1993
362087Sbostic * The Regents of the University of California. All rights reserved.
440411Sbostic *
540412Sbostic * This code is derived from software contributed to Berkeley by
640412Sbostic * Adam de Boor.
740411Sbostic *
842741Sbostic * %sccs.include.redist.c%
940411Sbostic */
1040412Sbostic
1140411Sbostic #ifndef lint
12*69094Schristos static char sccsid[] = "@(#)lstRemove.c 8.2 (Berkeley) 04/28/95";
1340412Sbostic #endif /* not lint */
1440411Sbostic
1540412Sbostic /*-
1640412Sbostic * LstRemove.c --
1740412Sbostic * Remove an element from a list
1840412Sbostic */
1940412Sbostic
2040411Sbostic #include "lstInt.h"
2140411Sbostic
2240411Sbostic /*-
2340411Sbostic *-----------------------------------------------------------------------
2440411Sbostic * Lst_Remove --
2540411Sbostic * Remove the given node from the given list.
2640411Sbostic *
2740411Sbostic * Results:
2840411Sbostic * SUCCESS or FAILURE.
2940411Sbostic *
3040411Sbostic * Side Effects:
3140411Sbostic * The list's firstPtr will be set to NilListNode if ln is the last
3240411Sbostic * node on the list. firsPtr and lastPtr will be altered if ln is
3340411Sbostic * either the first or last node, respectively, on the list.
3440411Sbostic *
3540411Sbostic *-----------------------------------------------------------------------
3640411Sbostic */
3740411Sbostic ReturnStatus
Lst_Remove(l,ln)3840411Sbostic Lst_Remove (l, ln)
3940411Sbostic Lst l;
4040411Sbostic LstNode ln;
4140411Sbostic {
4240411Sbostic register List list = (List) l;
4340411Sbostic register ListNode lNode = (ListNode) ln;
4440411Sbostic
4540411Sbostic if (!LstValid (l) ||
4640411Sbostic !LstNodeValid (ln, l)) {
4740411Sbostic return (FAILURE);
4840411Sbostic }
4940411Sbostic
5040411Sbostic /*
5140411Sbostic * unlink it from the list
5240411Sbostic */
5340411Sbostic if (lNode->nextPtr != NilListNode) {
5440411Sbostic lNode->nextPtr->prevPtr = lNode->prevPtr;
5540411Sbostic }
5640411Sbostic if (lNode->prevPtr != NilListNode) {
5740411Sbostic lNode->prevPtr->nextPtr = lNode->nextPtr;
5840411Sbostic }
5940411Sbostic
6040411Sbostic /*
6140411Sbostic * if either the firstPtr or lastPtr of the list point to this node,
6240411Sbostic * adjust them accordingly
6340411Sbostic */
6440411Sbostic if (list->firstPtr == lNode) {
6540411Sbostic list->firstPtr = lNode->nextPtr;
6640411Sbostic }
6740411Sbostic if (list->lastPtr == lNode) {
6840411Sbostic list->lastPtr = lNode->prevPtr;
6940411Sbostic }
7040411Sbostic
7140411Sbostic /*
7240411Sbostic * Sequential access stuff. If the node we're removing is the current
7340411Sbostic * node in the list, reset the current node to the previous one. If the
7440411Sbostic * previous one was non-existent (prevPtr == NilListNode), we set the
7540411Sbostic * end to be Unknown, since it is.
7640411Sbostic */
7740411Sbostic if (list->isOpen && (list->curPtr == lNode)) {
7840411Sbostic list->curPtr = list->prevPtr;
7940411Sbostic if (list->curPtr == NilListNode) {
8040411Sbostic list->atEnd = Unknown;
8140411Sbostic }
8240411Sbostic }
8340411Sbostic
8440411Sbostic /*
8540411Sbostic * the only way firstPtr can still point to ln is if ln is the last
8640411Sbostic * node on the list (the list is circular, so lNode->nextptr == lNode in
8740411Sbostic * this case). The list is, therefore, empty and is marked as such
8840411Sbostic */
8940411Sbostic if (list->firstPtr == lNode) {
9040411Sbostic list->firstPtr = NilListNode;
9140411Sbostic }
9240411Sbostic
9340411Sbostic /*
9440411Sbostic * note that the datum is unmolested. The caller must free it as
9540411Sbostic * necessary and as expected.
9640411Sbostic */
9740411Sbostic if (lNode->useCount == 0) {
9840411Sbostic free ((Address)ln);
9940411Sbostic } else {
10040411Sbostic lNode->flags |= LN_DELETED;
10140411Sbostic }
10240411Sbostic
10340411Sbostic return (SUCCESS);
10440411Sbostic }
10540411Sbostic
106