< previous page page_a76 next page >

Page A76
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
int OrdList::Length() const
{
    NodePtr currPtr = head;    // Loop control pointer
    int     count = 0;         // Number of nodes in list

    while (currPtr != NULL)
    {
            // Invariant (prior to test):
            //     count == number of nodes preceding *currPtr
            // && currPtr points to a list node or == NULL

        count++;
        currPtr = currPtr->link;
    }
    return count;
}
5. In each function, after the statement
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
newNodePtr = new NodeType;
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
insert the following:
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
if (newNodePtr == NULL)
{
    cout << Not enough memory to insert  << item << endl;
    exit(1);
}
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Change the postcondition for InsertTop to the following:
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
//IF not enough memory to add item to list
//     Program has halted with an error message
// ELSE
//     New node containing item is inserted at top of linked list
//  && component members of list nodes are in ascending order
Change the postcondition for Insert to the following:
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
// IF not enough memory to add item to list
//     Program has halted with an error message
// ELSE
//     New node containing item is inserted into its proper place
//     in linked list
//  && component members of list nodes are in ascending order

 
< previous page page_a76 next page >