/*- *----------------------------------------------------------------------- * 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_Remove -- * Remove the given node from the given list. * * Results: * SUCCESS or FAILURE. * * Side Effects: * The list's firstPtr will be set to NULL if ln is the last * node on the list. firsPtr and lastPtr will be altered if ln is * either the first or last node, respectively, on the list. * *----------------------------------------------------------------------- */ ReturnStatus Lst_Remove(Lst l, LstNode ln) { List list = l; ListNode lNode = ln; if (!LstValid (l) || !LstNodeValid (ln, l)) { return (FAILURE); } /* * unlink it from the list */ if (lNode->nextPtr != NULL) { lNode->nextPtr->prevPtr = lNode->prevPtr; } if (lNode->prevPtr != NULL) { lNode->prevPtr->nextPtr = lNode->nextPtr; } /* * if either the firstPtr or lastPtr of the list point to this node, * adjust them accordingly */ if (list->firstPtr == lNode) { list->firstPtr = lNode->nextPtr; } if (list->lastPtr == lNode) { list->lastPtr = lNode->prevPtr; } /* * Sequential access stuff. If the node we're removing is the current * node in the list, reset the current node to the previous one. If the * previous one was non-existent (prevPtr == NULL), we set the * end to be Unknown, since it is. */ if (list->isOpen && (list->curPtr == lNode)) { list->curPtr = list->prevPtr; if (list->curPtr == NULL) { list->atEnd = Unknown; } } /* * the only way firstPtr can still point to ln is if ln is the last * node on the list (the list is circular, so lNode->nextptr == lNode in * this case). The list is, therefore, empty and is marked as such */ if (list->firstPtr == lNode) { list->firstPtr = NULL; } /* * note that the datum is unmolested. The caller must free it as * necessary and as expected. */ if (lNode->useCount == 0) { free(ln); } else { lNode->flags |= LN_DELETED; } return (SUCCESS); }