|
|
|
|
|
|
|
list1.Insert(352);
list1.Insert(48);
list2.Insert(12);
.
.
.
if ( !list2.IsEmpty() )
list2.DeleteTop(item); |
|
|
|
|
|
|
|
|
then each of the two objects list1 and list2 has its own private head variable and maintains its own dynamic linked list on the free store. |
|
|
|
|
|
|
|
|
In Figure 18-6, the specification file ordlist.h declares a type NodeType, but only as a forward declaration. The only reason we need to declare the identifier NodeType in the specification file is so that the data type of the private variable head can be specified. In the spirit of information hiding, we place the complete declaration of NodeType into the implementation file ordlist.cpp. The complete declaration is an implementation detail that the user does not need to know about. Here's how ordlist.cpp starts out: |
|
|
|
|
|
|
|
|
//******************************************************************
// IMPLEMENTATION FILE (ordlist.cpp)
// This file implements the OrdList class member functions
// List representation: a linked list of dynamic nodes.
//******************************************************************
#include "ordlist.h"
#include <iostream.h>
#include <stddef.h> // For NULL
typedef NodeType* NodePtr;
struct NodeType
{
ComponentType component;
NodePtr link;
};
// Private members of class:
// NodePtr head; External pointer to linked list
.
.
. |
|
|
|
|
|
|
|
|
To illustrate some commonly used algorithms on dynamic linked lists, let's look at the implementations of the OrdList member functions. Creating an empty linked list is the easiest of the algorithms, so we begin there. |
|
|
|
|
|