< previous page page_415 next page >

Page 415
LISTING 23.2 continued
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
103:        virtual ~InternalNode(){ delete myNext; delete myObject; }
104:        virtual Node<T> * Insert(T * theObject);
105:        virtual void Show() // delegate!
106:        {
107:           myObject->Show(); myNext->Show();
108:        }
109:     private:
110:        T * myObject;  // the Object itself
111:        Node<T> * myNext; // points to next node in the linked list
112:     };
113:
114:     // All the constructor does is initialize
115:     template <class T>
116:     InternalNode<T>::InternalNode(T * theObject, Node<T> * next):
117:     myObject(theObject),myNext(next)
118:     {
119:     }
120:
121:     // the meat of the list
122:     // When you put a new object into the list
123:     // it is passed to the node which figures out
124:     // where it goes and inserts it into the list
125:     template <class T>
126:     Node<T> * InternalNode<T>::Insert(T * theObject)
127:     {
128:
129:        // is the new guy bigger or smaller than me?
130:        int result = myObject->Compare(*theObject);
131:
132:
133:        switch(result)
134:        {
135:        // by convention if it is the same as me it comes first
136:        case kIsSame:        // fall through
137:        case kIsLarger:    // new Object comes before me
138:             {
139:                 InternalNode<T> * ObjectNode =
140:                 new InternalNode<T>(theObject, this);
141:                 return ObjectNode;
142:             }
143:
144:             // it is bigger than I am so pass it on to the next
145:             // node and let HIM handle it.
146:         case kIsSmaller:
147:                 myNext = myNext->Insert(theObject);
148:                 return this;
149:         }
150:         return this;  // appease MSC
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
continues

 
< previous page page_415 next page >

If you like this book, buy it!