while (currPtr != NULL &&
item > currPtr->component)
newNodePtr->link = currPtr;
if (prevPtr == NULL)
prevPtr->link = newNodePtr;
Because currPtr equals NULL, the expression is FALSE and the loop body is not repeated.
NULL is copied into link member of *newNodePtr.
Because prevPtr does not equal NULL, the else-clause is executed. Node *newNodePtr is inserted after *prevPtr. The list is shown with auxiliary variables removed.
You may have noticed in both the InsertTop and the Insert functions that we allocate a new dynamic node without checking to see whether the allocation succeeded. It is possible for the client code to insert so many items that the free store becomes full. On most systems, the free store is very large, but it is risky to assume that the new operation will always succeed. Programming Warm-Up Exercise 5 asks you to address this issue.
Deleting from a Linked List
To delete an existing node from a linked list, we have to loop through the nodes until we find the node we want to delete. We look at the mirror image of our insertions: deleting the top node and deleting a node whose component is equal to an incoming parameter.
To delete the first node, we just change the external pointer to point to the second node (or to contain NULL if we are deleting the only node in a onenode list). The value in the node being deleted can be returned as an outgoing parameter. Notice the precondition for the following function: the client must not call the function if the list is empty.
void OrdList::DeleteTop( /* out */ ComponentType& item )
// Precondition:
// Linked list is not empty (head != NULL)
// && component members of list nodes are in ascending order