< previous page page_413 next page >

Page 413
LISTING 23.2 continued
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
11:      // linked lists. The list delegates to the node.
12:      // The node is an abstract Object type. Three types of
13:      // nodes are used, head nodes, tail nodes and internal
14:      // nodes. Only the internal nodes hold Object.
15:      //
16:      // The Object 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:
25:      enum { kIsSmaller, kIsLarger, kIsSame};
26:
27:      // Object class to put into the linked list
28:      // Any class in this linked list must support two methods:
29:      // Show (displays the value) and Compare // (returns relative position)
30:      class Data
31:      {
32:      public:
33:           Data(int val):myValue(val){}
34:           ~Data()
35:           {
36:              cout <<  Deleting Data object with value:  ;
37:              cout << myValue <<  \n ;
38:           }
39:           int Compare(const Data &);
40:           void Show() { cout << myValue << end1; }
41:      private:
42:           int myValue;
43:      };
44:
45:      // compare is used to decide where in the list
46:      // a particular object belongs.
47:      int Data::Compare(const Data & theOtherObject)
48:      {
49:           if (myValue < theOtherObject.myValue)
50:              return kIsSmaller;
51:           if (myValue > theOtherObject.myValue)
52:              return kIsLarger;
53:           else
54:              return kIsSame;
55:      }
56:
57:      // Another class to put into the linked list
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
continues

 
< previous page page_413 next page >

If you like this book, buy it!