|
|
|
|
|
|
|
p, q, and r point to nodes in a dynamic linked list. The nodes themselves are *p, *q, and *r. Use the preceding diagram to convince yourself that the following relations are true. |
|
|
|
|
|
|
|
|
p->link == q
*(p->link) == *q
p->link->link == r
*(p->link->link) == *r
q->link == r
*(q->link) == *r |
|
|
|
|
|
|
|
|
And remember the semantics of assignment statements for pointers. |
|
|
|
|
| |
|
|
|
|
Assigns the contents of pointer q to pointer p.
Assigns the contents of the variable pointed to by q to the variable pointed to by p. |
|
|
|
|
|
|
|
|
|
|
Classes and Dynamic Linked Lists |
|
|
|
|
|
|
|
|
In Chapter 17, we said that classes whose objects manipulate dynamic data on the free store should provide not only a class constructor but also a destructor, a deep copy operation, and a copy-constructor. The OrdList class includes all of these except (to keep the example simpler) a deep copy operation. Let's look at the class destructor. |
|
|
|
|
|
|
|
|
The purpose of the destructor is to deallocate the dynamic linked list when an OrdList class object is destroyed. Without a destructor, the linked list would be left behind on the free store, still allocated but inaccessible. The code for the destructor is easy to write. Using the existing member functions IsEmpty and DeleteTop, we simply march through the list and delete each node: |
|
|
|
|
|
|
|
|
OrdList::~OrdList()
// Destructor
// Postcondition:
// All linked list nodes have been deallocated from free store
{
ComponentType temp; // Temporary variable |
|
|
|
|
|