home *** CD-ROM | disk | FTP | other *** search
- #include <iostream.h>
- #include <afx.h>
-
- // Boolean
- typedef unsigned char Boolean;
-
-
- // LIST NODE
- template <class KeyType, class ValType>
- class ListNodeT {
- public:
- ListNodeT(KeyType NewKey, ValType& NewObject);
- void SetNext(ListNodeT* pNewNext);
- ValType* GetContents() const;
- KeyType GetKey() const;
- ListNodeT* GetNext() const;
-
- private:
- ListNodeT* next;
- ValType* value;
- KeyType key;
- };
-
- // Constructor
- template <class KeyType, class ValType>
- ListNodeT<KeyType, ValType>::ListNodeT(KeyType NewKey,
- ValType& NewObject) :
-
- // Initialize member data
- next(NULL),
- value(&NewObject),
- key(NewKey)
- { }
-
- // Public member functions
- template <class KeyType, class ValType>
- void ListNodeT<KeyType, ValType>::SetNext(ListNodeT* pNewNext)
- {
- next = pNewNext;
- }
-
- template <class KeyType, class ValType>
- ValType* ListNodeT<KeyType, ValType>::GetContents() const
- {
- return value;
- }
-
- template <class KeyType, class ValType>
- KeyType ListNodeT<KeyType, ValType>::GetKey() const
- {
- return key;
- }
-
- template <class KeyType, class ValType>
- ListNodeT<KeyType, ValType>* ListNodeT<KeyType, ValType>::GetNext() const
- {
- return next;
- }
-
-
- // LIST
- template <class KeyType, class ValType>
- class ListT {
- public:
- ListT();
- ~ListT();
- Boolean Add(KeyType NewKey, ValType& NewObject);
- Boolean Remove(KeyType SearchKey);
- ValType* Find(KeyType SearchKey) const;
- ValType* operator[](int Position);
- int GetListSize() const;
-
- private:
- ListNodeT<KeyType, ValType>* head;
- unsigned int NumItems;
- };
-
- // Constructor
- template <class KeyType, class ValType>
- ListT<KeyType, ValType>::ListT() :
-
- // Initialize member data
- head(NULL),
- NumItems(0)
- { }
-
- // Destructor
- template <class KeyType, class ValType>
- ListT<KeyType, ValType>::~ListT()
- {
- // Delete all the nodes in the list
- while (head) {
- ListNodeT<KeyType, ValType>* pTemp = head->GetNext();
- delete head;
- head = pTemp;
- }
- }
-
- // Public member functions
- template <class KeyType, class ValType>
- Boolean ListT<KeyType, ValType>::Add(KeyType NewKey,
- ValType& NewObject)
- {
- // Allocate memory for the new node
- ListNodeT<KeyType, ValType>* pNewNode =
- new ListNodeT<KeyType, ValType>(NewKey, NewObject);
- if (!pNewNode)
- return FALSE;
-
- // Insert the node into the list
- pNewNode->SetNext(head);
- head = pNewNode;
- NumItems++;
- return TRUE;
- }
-
- template <class KeyType, class ValType>
- Boolean ListT<KeyType, ValType>::Remove(KeyType SearchKey)
- {
- ListNodeT<KeyType, ValType>* 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) {
- ListNodeT<KeyType, ValType>* pTemp =
- pCursor->GetNext();
- pCursor->SetNext(pTemp->GetNext());
- delete pTemp;
- NumItems--;
- return TRUE;
- }
- }
- return FALSE;
- }
-
- template <class KeyType, class ValType>
- ValType* ListT<KeyType, ValType>::Find(KeyType SearchKey) const
- {
- ListNodeT<KeyType, ValType>* pCursor = head;
- while (pCursor) {
- if (pCursor->GetKey() == SearchKey)
- return pCursor->GetContents();
- else
- pCursor = pCursor->GetNext();
- }
- return NULL;
- }
-
- template <class KeyType, class ValType>
- int ListT<KeyType, ValType>::GetListSize() const
- {
- return NumItems;
- }
-
- // Operators
- template <class KeyType, class ValType>
- ValType* ListT<KeyType, ValType>::operator[](int Position)
- {
- ListNodeT<KeyType, ValType>* pCursor = head;
- int Count = 1;
- while (pCursor) {
- if (Count++ == Position)
- return pCursor->GetContents();
- pCursor = pCursor->GetNext();
- }
- return NULL;
- }
-
- // Now use it all
-
- template <class T>
- void ShowList(T& theList)
- {
- int Loop;
- cout << "The list: ( ";
- for (Loop = 0; Loop < theList.GetListSize(); Loop++) {
- if (Loop) cout << ", ";
- cout << *theList[Loop+1];
- }
- cout << " )\n";
- }
-
- void main()
- {
- int Int1 = 34, Int2 = 22, Int3 = 675;
- ListT<int, int> theIntList;
- theIntList.Add(1, Int1);
- theIntList.Add(2, Int2);
- theIntList.Add(3, Int3);
- ShowList(theIntList);
- theIntList.Remove(2);
- ShowList(theIntList);
- theIntList.Remove(1);
- ShowList(theIntList);
- theIntList.Remove(3);
- ShowList(theIntList);
-
- CString Str1("Here we are"), Str2("There you go"),
- Str3("What up?");
- ListT<CString, CString> theStrList;
- theStrList.Add("Bob quote", Str1);
- theStrList.Add("Frank quote", Str2);
- theStrList.Add("Sally quote", Str3);
- ShowList(theStrList);
- theStrList.Remove("Frank quote");
- ShowList(theStrList);
- theStrList.Remove("Bob quote");
- ShowList(theStrList);
- theStrList.Remove("Sally quote");
- ShowList(theStrList);
- }
-