|
|
 |
|
|
|
|
219:
220: // now walk the list and show the data
221: ll.ShowAll();
222: return 0; // ll falls out of scope and is destroyed!
223: } |
|
|
|
|
|
|
|
|
What value? (0 to stop): 5
What value? (0 to stop): 8
What value? (0 to stop): 3
What value? (0 to stop): 9
What value? (0 to stop): 2
What value? (0 to stop): 10
What value? (0 to stop): 0
2
3
5
8
9
10 |
|
|
|
|
|
|
|
|
Analysis: The first thing to note is the enumerated constant, which provides three constant values: kIsSmaller, kIsLarger, and kIsSame. Every object that may be held in this linked list must support a Compare() method. These constants will be the result value returned by the Compare() method. |
|
|
|
|
|
|
|
|
For illustration purposes, the class Data is created on lines 2938, and the Compare() method is implemented on lines 4050. A Data object holds a value and can compare itself with other Data objects. It also supports a Show() method to display the value of the Data object. |
|
|
|
|
|
|
|
|
The easiest way to understand the workings of the linked list is to step through an example of using one. On line 202 a driver program is declared, on line 204 a pointer to a Data object is declared, and on line 206 a local linked list is defined. |
|
|
|
|
|
|
|
|
When the linked list is created, the constructor on line 190 is called. The only work done in the constructor is to allocate a HeadNode object and to assign that object's address to the pointer held in the linked list on line 183. |
|
|
|
|
|
|
|
|
This allocation of a HeadNode invokes the HeadNode constructor shown on line 161. This in turn allocates a TailNode and assigns its address to the head node's myNext pointer. The creation of the TailNode calls the TailNode constructor shown on line 129, which is inline and which does nothing. |
|
|
|
|
|