/*VARARGS2*/ int Lst_ForEachFrom(Lst l, LstNode ln, int (*proc)(void *, void *), void *d) { ListNode tln = ln; List list = l; ListNode next; Boolean done; int result; if (!LstValid (list) || LstIsEmpty (list)) { return 0; } do { /* * Take care of having the current element deleted out from under * us. */ next = tln->nextPtr; /* * We're done with the traversal if * - the next node to examine is the first in the queue or * doesn't exist and * - nothing's been added after the current node (check this * after proc() has been called). */ done = (next == NULL || next == list->firstPtr); (void) tln->useCount++; result = (*proc) (tln->datum, d); (void) tln->useCount--; /* * Now check whether a node has been added. * Note: this doesn't work if this node was deleted before * the new node was added. */ if (next != tln->nextPtr) { next = tln->nextPtr; done = 0; } if (tln->flags & LN_DELETED) { free((char *)tln); } tln = next; } while (!result && !LstIsEmpty(list) && !done); return result; }
/*- *----------------------------------------------------------------------- * Lst_FindFrom -- * Search for a node starting and ending with the given one on the * given list using the passed datum and comparison function to * determine when it has been found. * * Results: * The found node or NILLNODE * * Side Effects: * None. * *----------------------------------------------------------------------- */ LstNode Lst_FindFrom(Lst l, LstNode ln, ClientData d, int (*cProc)(ClientData, ClientData)) { ListNode tln; Boolean found = FALSE; if (!LstValid (l) || LstIsEmpty (l) || !LstNodeValid (ln, l)) { return (NILLNODE); } tln = (ListNode)ln; do { if ((*cProc) (tln->datum, d) == 0) { found = TRUE; break; } else { tln = tln->nextPtr; } } while (tln != (ListNode)ln && tln != NilListNode); if (found) { return ((LstNode)tln); } else { return (NILLNODE); } }
/*- *----------------------------------------------------------------------- * Lst_InsertBefore -- * Insert a new node with the given piece of data before the given * node in the given list. * * Input: * l list to manipulate * ln node before which to insert d * d datum to be inserted * * Results: * SUCCESS or FAILURE. * * Side Effects: * the firstPtr field will be changed if ln is the first node in the * list. * *----------------------------------------------------------------------- */ ReturnStatus Lst_InsertBefore(Lst l, LstNode ln, void *d) { ListNode nLNode; /* new lnode for d */ ListNode lNode = ln; List list = l; /* * check validity of arguments */ if (LstValid (l) && (LstIsEmpty (l) && ln == NULL)) goto ok; if (!LstValid (l) || LstIsEmpty (l) || !LstNodeValid (ln, l)) { return (FAILURE); } ok: PAlloc (nLNode, ListNode); nLNode->datum = d; nLNode->useCount = nLNode->flags = 0; if (ln == NULL) { if (list->isCirc) { nLNode->prevPtr = nLNode->nextPtr = nLNode; } else { nLNode->prevPtr = nLNode->nextPtr = NULL; } list->firstPtr = list->lastPtr = nLNode; } else { nLNode->prevPtr = lNode->prevPtr; nLNode->nextPtr = lNode; if (nLNode->prevPtr != NULL) { nLNode->prevPtr->nextPtr = nLNode; } lNode->prevPtr = nLNode; if (lNode == list->firstPtr) { list->firstPtr = nLNode; } } return (SUCCESS); }
/*- *----------------------------------------------------------------------- * Lst_InsertAfter -- * Create a new node and add it to the given list after the given node. * * Input: * l affected list * ln node after which to append the datum * d said datum * * Results: * SUCCESS if all went well. * * Side Effects: * A new ListNode is created and linked in to the List. The lastPtr * field of the List will be altered if ln is the last node in the * list. lastPtr and firstPtr will alter if the list was empty and * ln was NILLNODE. * *----------------------------------------------------------------------- */ ReturnStatus Lst_InsertAfter(Lst l, LstNode ln, ClientData d) { List list; ListNode lNode; ListNode nLNode; if (LstValid (l) && (ln == NILLNODE && LstIsEmpty (l))) { goto ok; } if (!LstValid (l) || LstIsEmpty (l) || ! LstNodeValid (ln, l)) { return (FAILURE); } ok: list = l; lNode = ln; PAlloc (nLNode, ListNode); nLNode->datum = d; nLNode->useCount = nLNode->flags = 0; if (lNode == NilListNode) { if (list->isCirc) { nLNode->nextPtr = nLNode->prevPtr = nLNode; } else { nLNode->nextPtr = nLNode->prevPtr = NilListNode; } list->firstPtr = list->lastPtr = nLNode; } else { nLNode->prevPtr = lNode; nLNode->nextPtr = lNode->nextPtr; lNode->nextPtr = nLNode; if (nLNode->nextPtr != NilListNode) { nLNode->nextPtr->prevPtr = nLNode; } if (lNode == list->lastPtr) { list->lastPtr = nLNode; } } return (SUCCESS); }
/*- *----------------------------------------------------------------------- * Lst_Last -- * Return the last node on the list l. * * Results: * The requested node or NILLNODE if the list is empty. * * Side Effects: * None. * *----------------------------------------------------------------------- */ LstNode Lst_Last(Lst l) { if (!LstValid(l) || LstIsEmpty (l)) { return (NILLNODE); } else { return (l->lastPtr); } }
/*- *----------------------------------------------------------------------- * Lst_Open -- * Open a list for sequential access. A list can still be searched, * etc., without confusing these functions. * * Results: * SUCCESS or FAILURE. * * Side Effects: * isOpen is set TRUE and curPtr is set to NULL so the * other sequential functions no it was just opened and can choose * the first element accessed based on this. * *----------------------------------------------------------------------- */ ReturnStatus Lst_Open(Lst l) { if (LstValid (l) == FALSE) { return (FAILURE); } (l)->isOpen = TRUE; (l)->atEnd = LstIsEmpty (l) ? Head : Unknown; (l)->curPtr = NULL; return (SUCCESS); }
/*- *----------------------------------------------------------------------- * Lst_IsEmpty -- * Return TRUE if the given list is empty. * * Results: * TRUE if the list is empty, FALSE otherwise. * * Side Effects: * None. * * A list is considered empty if its firstPtr == NilListNode (or if * the list itself is NILLIST). *----------------------------------------------------------------------- */ Boolean Lst_IsEmpty(Lst l) { return ( ! LstValid (l) || LstIsEmpty(l)); }