< previous page page_1062 next page >

Page 1062
Creating an Empty Linked List
To create a linked list with no nodes, all that is necessary is to assign the external pointer the value NULL. For the OrdList class, the appropriate place to do this is in the class constructor:
OrdList::OrdList()

// Constructor

// Postcondition:
//     head == NULL

{
    head = NULL;
}
One thing you will notice as we go through the OrdList member functions is that the implementation assertions (the preconditions and postconditions appearing in the implementation file) are often stated differently from the abstract assertions (those located in the specification file). Abstract assertions are written in terms that are meaningful to the user of the ADT; implementation details should not be mentioned. In contrast, implementation assertions can be made more precise by referring directly to variables and algorithms in the implementation code. In the case of the OrdList class constructor, the abstract postcondition is simply that an empty list (not a linked list) has been created. On the other hand, the implementation postcondition
// Postcondition:
//     head == NULL
is phrased in terms of our private data (head) and our particular list implementation (a dynamic linked list).
Testing for an Empty Linked List
The OrdList member function IsEmpty returns TRUE if the list is empty and FALSE if the list is not empty. Using a dynamic linked list representation, we return TRUE if head contains the value NULL, and FALSE otherwise:
Boolean OrdList::IsEmpty() const

// Postcondition:
//     Function value == TRUE, if head == NULL
//                    == FALSE, otherwise

{
    return (head == NULL);
}

 
< previous page page_1062 next page >