|
|
|
|
|
|
|
A well-designed machine has lots of small, well-understood parts, each doing its own job and working together to accomplish a greater good. A well-designed program is much the same: each class sticks to its own knitting, but together they create a heck of an afghan. |
|
|
|
|
|
|
|
|
The linked list will consist of nodes. The node class itself will be abstract; we'll use three subtypes to accomplish the work. There will be a head node whose job is to manage the head of the list, a tail node (guess what its job is!), and zero or more internal nodes. The internal nodes will keep track of the actual data to be held in the list. |
|
|
|
|
|
|
|
|
Note that the data and the list are quite distinct. You can, in theory, save any type of data you like in a list. It isn't the data that is linked together; it is the node that holds the data. |
|
|
|
|
|
|
|
|
The driver program doesn't know about the nodes; it works with the list. The list, however, does little work; it simply delegates to the nodes. |
|
|
|
|
|
|
|
|
Listing 19.1 shows the code; we'll examine it in excruciating detail: |
|
|
|
 |
|
|
|
|
1: // ***********************************************
2: // FILE: Listing 19.1
3: //
4: // PURPOSE: Demonstrate ilinked list
5: // NOTES:
6: //
7: // COPYRIGHT: Copyright (C) 1997 Liberty Associates, Inc.
8: // All Rights Reserved
9: //
10: // Demonstrates an object-oriented approach to
11: // linked lists. The list delegates to the node.
12: // The node is an abstract data type. Three types of
13: // nodes are used, head nodes, tail nodes and internal
14: // nodes. Only the internal nodes hold data.
15: //
16: // The Data class is created to serve as an object to
17: // hold in the linked list.
18: //
19: // ***********************************************
20:
21:
22: #include <iostream.h>
23:
24: enum { kIsSmaller, kIsLarger, kIsSame};
25: |
|
|
|
 |
|
|
|
|
continues |
|
|
|
|
|