home *** CD-ROM | disk | FTP | other *** search
/ Using Visual C++ 4 (Special Edition) / Using_Visual_C_4_Special_Edition_QUE_1996.iso / ch14 / inhlist.cpp < prev    next >
C/C++ Source or Header  |  1995-09-18  |  4KB  |  204 lines

  1. #include <iostream.h>
  2.  
  3. // Boolean type
  4. typedef unsigned char Boolean;
  5. const Boolean TRUE = 1;
  6. const Boolean FALSE = 0;
  7.  
  8.  
  9. class BaseObject {
  10. // Empty
  11. };
  12.  
  13.  
  14. class ListNode {
  15. public:
  16.     ListNode(int NewKey, BaseObject& NewObject);
  17.     void SetNext(ListNode* pNewNext);
  18.     BaseObject* GetContents() const;
  19.     int GetKey() const;
  20.     ListNode* GetNext() const;
  21.  
  22. private:
  23.     ListNode* next;
  24.     BaseObject* value;
  25.     int key;
  26. };
  27.  
  28. // Constructor
  29. ListNode::ListNode(int NewKey, BaseObject& NewObject) :
  30.  
  31.     // Initialize member data
  32.     next(NULL),
  33.     value(&NewObject),
  34.     key(NewKey)
  35. { }
  36.  
  37. // Public member functions
  38. void ListNode::SetNext(ListNode* pNewNext)
  39. {
  40.     next = pNewNext;
  41. }
  42.  
  43. BaseObject* ListNode::GetContents() const
  44. {
  45.     return value;
  46. }
  47.  
  48. int ListNode::GetKey() const
  49. {
  50.     return key;
  51. }
  52.  
  53. ListNode* ListNode::GetNext() const
  54. {
  55.     return next;
  56. }
  57.  
  58.  
  59. class List {
  60. public:
  61.     List();
  62.     ~List();
  63.     Boolean Add(int NewKey, BaseObject& NewObject);
  64.     Boolean Remove(int SearchKey);
  65.     BaseObject* Find(int SearchKey) const;
  66.     BaseObject* operator[](int OrderKey);
  67.     int GetListSize() const;
  68.  
  69. private:
  70.     ListNode* head;
  71.     unsigned int NumItems;
  72. };
  73.  
  74. // Constructor
  75. List::List() :
  76.  
  77.     // Initialize member data
  78.     head(NULL),
  79.     NumItems(0)
  80. { }
  81.  
  82. // Destructor
  83. List::~List()
  84. {
  85.     // Delete all the nodes in the list
  86.     while (head) {
  87.         ListNode* pTemp = head->GetNext();
  88.         delete head;
  89.         head = pTemp;
  90.     }
  91. }
  92.  
  93. // Public member functions
  94. Boolean List::Add(int NewKey, BaseObject& NewObject)
  95. {
  96.     // Allocate memory for the new node
  97.     ListNode* pNewNode = new ListNode(NewKey, NewObject);
  98.     if (!pNewNode)
  99.         return FALSE;
  100.  
  101.     // Insert the node into the list
  102.     pNewNode->SetNext(head);
  103.     head = pNewNode;
  104.     NumItems++;
  105.     return TRUE;
  106. }
  107.  
  108. Boolean List::Remove(int SearchKey)
  109. {
  110.     ListNode* pCursor = head;
  111.  
  112.     // Is there a list?
  113.     if (!pCursor)
  114.         return FALSE;
  115.  
  116.     // Check the head first
  117.     if (pCursor->GetKey() == SearchKey) {
  118.         head = pCursor->GetNext();
  119.         delete pCursor;
  120.         NumItems--;
  121.         return TRUE;
  122.     }
  123.  
  124.     // Scan the list
  125.     while (pCursor->GetNext()) {
  126.         if (pCursor->GetNext()->GetKey() == SearchKey) {
  127.             ListNode* pTemp = pCursor->GetNext();
  128.             pCursor->SetNext(pTemp->GetNext());
  129.             delete pTemp;
  130.             NumItems--;
  131.             return TRUE;
  132.         }
  133.     }
  134.     return FALSE;
  135. }
  136.  
  137. BaseObject* List::Find(int SearchKey) const
  138. {
  139.     ListNode* pCursor = head;
  140.     while (pCursor) {
  141.         if (pCursor->GetKey() == SearchKey)
  142.             return pCursor->GetContents();
  143.         else
  144.             pCursor = pCursor->GetNext();
  145.     }
  146.     return NULL;
  147. }
  148.  
  149. int List::GetListSize() const
  150. {
  151.     return NumItems;
  152. }
  153.  
  154. // Operators
  155. BaseObject* List::operator[](int OrderKey)
  156. {
  157.     ListNode* pCursor = head;
  158.     int Count = 1;
  159.     while (pCursor) {
  160.         if (Count++ == OrderKey)
  161.             return pCursor->GetContents();
  162.         pCursor = pCursor->GetNext();
  163.     }
  164.     return NULL;
  165. }
  166.  
  167.  
  168. // Now use it!
  169.  
  170.  
  171. struct IntClass : public BaseObject {
  172.     IntClass(int NewInt) { theInt = NewInt; }
  173.     int theInt;
  174. };
  175.  
  176. void ShowList(List& theList)
  177. {
  178.     int Loop;
  179.     cout << "The list: ( ";
  180.     for (Loop = 0; Loop < theList.GetListSize(); Loop++) {
  181.         if (Loop) cout << ", ";
  182.         IntClass* pIntClass = (IntClass*) theList[Loop+1];
  183.         cout << pIntClass->theInt;
  184.     }
  185.     cout << " )\n";
  186. }
  187.  
  188. void main()
  189. {
  190.     List theList;
  191.     IntClass Int1(34), Int2(22), Int3(675);
  192.  
  193.     theList.Add(1, Int1);
  194.     theList.Add(2, Int2);
  195.     theList.Add(3, Int3);
  196.     ShowList(theList);
  197.     theList.Remove(2);
  198.     ShowList(theList);
  199.     theList.Remove(1);
  200.     ShowList(theList);
  201.     theList.Remove(3);
  202.     ShowList(theList);
  203. }
  204.