home *** CD-ROM | disk | FTP | other *** search
- // SLL.H - Singly linked list template class
- //SLL.H - SLL represents a singly linked list container // Note 1
- // template class.
-
- // SLLNode - represents a node in the linked list chain.
- // Each node contains a pointer to the next node
- // in the list plus a pointer to the data.
- template <class T> // Note 2
- class SLLNode
- {
- SLLNode<T>(T* pD)
- {
- pNext = 0;
- pData = pD;
- }
-
- SLLNode<T>* pNext; // pointer to next node // Note 3
- T* pData; // pointer to data
- };
-
- // SLL - this class represents a single list class. // Note 2
- // A singly linked list consists of a list of nodes
- // each of which points to the data.
- template <class T>
- class SLL
- {
- protected:
- SLLNode<T>* pFirst; // pointer to first node // Note 4
- SLLNode<T>* pLast; // pointer to last node
- SLLNode<T>* pCurrent; // pointer to "current" node
- int nMembers; // number of members in list
-
- public:
- SLL();
-
- // add - add member to end of list
- void add(T* pData)
- {
- // create a node which points to our data
- SLLNode<T>* pNode = new SLLNode<T>(pData); // Note 5
-
- // add it to the end of the list
- if (pLast)
- {
- pLast->pNext = pNode;
- }
- pLast = pNode;
-
- // if this is the only member in the list...
- if (pFirst == 0)
- {
- // ...make it first member also
- pFirst = pNode;
- }
-
- // now count it
- nMembers++;
- }
-
- // count - return the number of accounts
- int count()
- {
- return nMembers;
- }
-
-
- // provide navigation functions
- // current - return the data pointer of current node
- T* current();
- // over - move over to the next node
- void over()
- {
- pCurrent = pCurrent->pNext;
- }
- // reset - reset to beginning of list
- void reset()
- {
- pCurrent = pFirst;
- }
- };
-
- // constructor - build an empty list
- template <class T>
- SLL<T>::SLL()
- {
- pFirst = pLast = (SLLNode<T>*)0;
- pCurrent = (SLLNode<T>*)0;
- nMembers = 0;
- }
-
- // current - return the data pointer of current node
- template <class T>
- T* SLL<T>::current()
- {
- // assume data pointer is zero...
- T* pData = (T*)0;
-
- // if we aren't off the end of list...
- if (pCurrent)
- {
- // ...then replace with actual data ptr
- pData = pCurrent->pData;
- }
- return pData;
- }
-
-