home *** CD-ROM | disk | FTP | other *** search
- #include <iostream.h>
-
- // Boolean type
- typedef unsigned char Boolean;
- const Boolean TRUE = 1;
- const Boolean FALSE = 0;
-
-
- class BaseObject {
- // Empty
- };
-
-
- class ListNode {
- public:
- ListNode(int NewKey, BaseObject& NewObject);
- void SetNext(ListNode* pNewNext);
- BaseObject* GetContents() const;
- int GetKey() const;
- ListNode* GetNext() const;
-
- private:
- ListNode* next;
- BaseObject* value;
- int key;
- };
-
- // Constructor
- ListNode::ListNode(int NewKey, BaseObject& NewObject) :
-
- // Initialize member data
- next(NULL),
- value(&NewObject),
- key(NewKey)
- { }
-
- // Public member functions
- void ListNode::SetNext(ListNode* pNewNext)
- {
- next = pNewNext;
- }
-
- BaseObject* ListNode::GetContents() const
- {
- return value;
- }
-
- int ListNode::GetKey() const
- {
- return key;
- }
-
- ListNode* ListNode::GetNext() const
- {
- return next;
- }
-
-
- class List {
- public:
- List();
- ~List();
- Boolean Add(int NewKey, BaseObject& NewObject);
- Boolean Remove(int SearchKey);
- BaseObject* Find(int SearchKey) const;
- BaseObject* operator[](int OrderKey);
- int GetListSize() const;
-
- private:
- ListNode* head;
- unsigned int NumItems;
- };
-
- // Constructor
- List::List() :
-
- // Initialize member data
- head(NULL),
- NumItems(0)
- { }
-
- // Destructor
- List::~List()
- {
- // Delete all the nodes in the list
- while (head) {
- ListNode* pTemp = head->GetNext();
- delete head;
- head = pTemp;
- }
- }
-
- // Public member functions
- Boolean List::Add(int NewKey, BaseObject& NewObject)
- {
- // Allocate memory for the new node
- ListNode* pNewNode = new ListNode(NewKey, NewObject);
- if (!pNewNode)
- return FALSE;
-
- // Insert the node into the list
- pNewNode->SetNext(head);
- head = pNewNode;
- NumItems++;
- return TRUE;
- }
-
- Boolean List::Remove(int SearchKey)
- {
- ListNode* pCursor = head;
-
- // Is there a list?
- if (!pCursor)
- return FALSE;
-
- // Check the head first
- if (pCursor->GetKey() == SearchKey) {
- head = pCursor->GetNext();
- delete pCursor;
- NumItems--;
- return TRUE;
- }
-
- // Scan the list
- while (pCursor->GetNext()) {
- if (pCursor->GetNext()->GetKey() == SearchKey) {
- ListNode* pTemp = pCursor->GetNext();
- pCursor->SetNext(pTemp->GetNext());
- delete pTemp;
- NumItems--;
- return TRUE;
- }
- }
- return FALSE;
- }
-
- BaseObject* List::Find(int SearchKey) const
- {
- ListNode* pCursor = head;
- while (pCursor) {
- if (pCursor->GetKey() == SearchKey)
- return pCursor->GetContents();
- else
- pCursor = pCursor->GetNext();
- }
- return NULL;
- }
-
- int List::GetListSize() const
- {
- return NumItems;
- }
-
- // Operators
- BaseObject* List::operator[](int OrderKey)
- {
- ListNode* pCursor = head;
- int Count = 1;
- while (pCursor) {
- if (Count++ == OrderKey)
- return pCursor->GetContents();
- pCursor = pCursor->GetNext();
- }
- return NULL;
- }
-
-
- // Now use it!
-
-
- struct IntClass : public BaseObject {
- IntClass(int NewInt) { theInt = NewInt; }
- int theInt;
- };
-
- void ShowList(List& theList)
- {
- int Loop;
- cout << "The list: ( ";
- for (Loop = 0; Loop < theList.GetListSize(); Loop++) {
- if (Loop) cout << ", ";
- IntClass* pIntClass = (IntClass*) theList[Loop+1];
- cout << pIntClass->theInt;
- }
- cout << " )\n";
- }
-
- void main()
- {
- List theList;
- IntClass Int1(34), Int2(22), Int3(675);
-
- theList.Add(1, Int1);
- theList.Add(2, Int2);
- theList.Add(3, Int3);
- ShowList(theList);
- theList.Remove(2);
- ShowList(theList);
- theList.Remove(1);
- ShowList(theList);
- theList.Remove(3);
- ShowList(theList);
- }
-