home *** CD-ROM | disk | FTP | other *** search
/ C++ for Dummies (3rd Edition) / C_FD.iso / CHAP26 / SLL.H < prev   
C/C++ Source or Header  |  1998-04-10  |  3KB  |  107 lines

  1. // SLL.H - Singly linked list template class
  2. //SLL.H - SLL represents a singly linked list container    // Note 1
  3. //        template class.
  4.  
  5. // SLLNode - represents a node in the linked list chain.
  6. //           Each node contains a pointer to the next node
  7. //           in the list plus a pointer to the data.
  8. template <class T>                                         // Note 2
  9. class SLLNode
  10. {
  11.      SLLNode<T>(T* pD)
  12.      {
  13.           pNext = 0;
  14.           pData = pD;
  15.      }
  16.  
  17.      SLLNode<T>* pNext;       // pointer to next node         // Note 3
  18.      T*          pData;       // pointer to data
  19. };
  20.  
  21. // SLL - this class represents a single list class.        // Note 2
  22. //       A singly linked list consists of a list of nodes
  23. //       each of which points to the data.
  24. template <class T>
  25. class SLL
  26. {
  27.   protected:
  28.     SLLNode<T>*  pFirst;     // pointer to first node      // Note 4
  29.     SLLNode<T>*  pLast;      // pointer to last node
  30.     SLLNode<T>*  pCurrent;   // pointer to "current" node
  31.     int nMembers;            // number of members in list
  32.  
  33.   public:
  34.     SLL();
  35.  
  36.     // add - add member to end of list
  37.     void add(T* pData)
  38.     {
  39.         // create a node which points to our data
  40.         SLLNode<T>* pNode = new SLLNode<T>(pData);         // Note 5
  41.  
  42.         // add it to the end of the list
  43.         if (pLast)
  44.         {
  45.             pLast->pNext = pNode;
  46.         }
  47.         pLast = pNode;
  48.  
  49.         // if this is the only member in the list...
  50.         if (pFirst == 0)
  51.         {
  52.             // ...make it first member also
  53.             pFirst = pNode;
  54.         }
  55.  
  56.         // now count it
  57.         nMembers++;
  58.     }
  59.  
  60.     // count - return the number of accounts
  61.     int count()
  62.     {
  63.         return nMembers;
  64.     }
  65.  
  66.  
  67.     // provide navigation functions
  68.     // current - return the data pointer of current node
  69.     T*  current();
  70.     // over - move over to the next node
  71.     void over()
  72.     {
  73.         pCurrent = pCurrent->pNext;
  74.     }
  75.     // reset - reset to beginning of list
  76.     void reset()
  77.     {
  78.         pCurrent = pFirst;
  79.     }
  80. };
  81.  
  82. // constructor - build an empty list
  83. template <class T>
  84. SLL<T>::SLL()
  85. {
  86.     pFirst = pLast = (SLLNode<T>*)0;
  87.     pCurrent = (SLLNode<T>*)0;
  88.     nMembers = 0;
  89. }
  90.  
  91. // current - return the data pointer of current node
  92. template <class T>
  93. T* SLL<T>::current()
  94. {
  95.     // assume data pointer is zero...
  96.     T* pData = (T*)0;
  97.  
  98.     // if we aren't off the end of list...
  99.     if (pCurrent)
  100.     {
  101.         // ...then replace with actual data ptr
  102.         pData = pCurrent->pData;
  103.     }
  104.     return pData;
  105. }
  106.  
  107.