< previous page page_1076 next page >

Page 1076
condition that the component we are looking for is definitely in the list, our loop control is simple. We don't have to worry about dereferencing the null pointer.
As in the Insert function, we need the node before the one that is to be deleted so we can change its link member. In the following function, we demonstrate another technique for keeping track of the previous node. Instead of comparing item with the component member of *currPtr, we compare it with the component member of the node pointed to by currPtr->link; that is, we compare item with currPtr->link->component. When currPtr->link->component is equal to item, *currPtr is the previous node.
void OrdList::Delete( /* in */ ComponentType item )

// Precondition:
//     item == component member of some list node
//  && List components are in ascending order
// Postcondition:
//     Node containing item is removed from linked list
//  && component members of list nodes are in ascending order

{
    NodePtr delPtr;     // Pointer to node to be deleted
    NodePtr currPtr;    // Loop control pointer

    // Check if item is in first node

    if (item == head->component)
    {
        // Delete first node

        delPtr = head;
        head = head->link;
    }
    else
    {
        // Search for node in rest of list

        currPtr = head;
        while (currPtr->link->component != item)

                // Invariant (prior to test):
                //     item != component member of any list node
                //     before *(currPtr->link)

            currPtr = currPtr->link;

 
< previous page page_1076 next page >