Ok, I'm getting somwhere now!
How do I change this linked list to look more like what you’ve said?
template <class T>
class Node
{
public:
Node(){}
virtual ~Node(){}
virtual Node * Insert(T * theObject) =0;
virtual void Show() = 0;
virtual void ShowNext() = 0;
private:
};
template <class T>
class InternalNode: public Node<T>
{
public:
InternalNode(T * theObject, Node<T> * next);
~InternalNode(){delete myNext; delete myObject;}
virtual Node<T> * Insert(T *theObject);
virtual void Show()
{
myObject->Show();system("PAUSE"); myNext->Show();
}
virtual void ShowNext()
{
// myObject->Show(); show first
}
private:
T * myObject; //the data
Node<T> * myNext; // the next bit of data in the list
};
//constructor
template <class T>
InternalNode<T>::InternalNode(T* theObject, Node<T> * next):
myObject(theObject), myNext(next)
{
}
//the main part of the list
// when u put new object in to the list
//it is passed to the node that figures out
//where it goes
template <class T>
Node<T> * InternalNode<T>::Insert(T * theObject)
{
//is the new data bigger or smaller
int result = myObject->Compare(*theObject);
switch(result)
{
// by convention if new data is same as old it goes 1st
case KIsSame: //fallthrough
case KIsLarger: //new data comes b4 old
{
InternalNode<T> * ObjectNode =
new InternalNode<T>(theObject, this);
return ObjectNode;
}
// if it is bigger than the old pass it on
//to the next node and let it handle it
case KIsSmaller:
myNext = myNext->Insert(theObject);
return this;
}
return this; //appease the compiler
}
//Tail node is just a sentinel
template <class T>
class TailNode : public Node<T>
{
public:
TailNode(){}
virtual ~TailNode(){}
virtual Node<T> * Insert(T * theObject);
virtual void Show(){}
virtual void ShowNext(){}
private:
};
//if data comes to here it must be b4
//because this is the tail and nothing comes after it
template <class T>
Node<T> * TailNode<T>::Insert(T * theObject)
{
InternalNode<T> * ObjectNode = new InternalNode<T>(theObject, this);
return ObjectNode;
}
//Head node has no data, it just points to the
// begining of the list
template <class T>
class HeadNode : public Node<T>
{
public:
HeadNode();
virtual ~HeadNode(){delete myNext;}
virtual Node<T> * Insert(T * theObject);
virtual void Show(){myNext->Show();}
virtual void ShowNext(){myNext->ShowNext();}
//private:
Node<T> * myNext;
};
//as soon as head is
//created so is the tail
template <class T>
HeadNode<T>::HeadNode()
{
myNext = new TailNode<T>;
}
//Nothing comes b4 the head so just
// pass the data to the next node
template <class T>
Node<T> * HeadNode<T>::Insert(T * theObject)
{
myNext = myNext->Insert(theObject);
return this;
}
//the linked list
template <class T>
class LinkedList
{
public:
LinkedList();
~LinkedList(){delete myHead;}
void Insert(T * theObject);
void ShowAll(){myHead->Show();}
//print out 1st node
void ShowFirst(){myHead->ShowNext();}
/* void ShowNext()
{
for(HeadNode<T> *temp = myHead; temp != NULL; temp = temp->myNext)
{
cout << temp->data;
}
}*/
private:
HeadNode<T> * myHead;
Node<T> * myNext;
};
//create the head node
//which creates the tail node
//so an empty list points to the head which
//points to the empty tail
template <class T>
LinkedList<T>::LinkedList()
{
myHead = new HeadNode<T>;
}
//delegation
template <class T>
void LinkedList<T>::Insert(T * pObject)
{
myHead->Insert(pObject);
}