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

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