//----------------------------------------------------------------------------- #ifndef LINKEDLISTCLASS_H #define LINKEDLISTCLASS_H //----------------------------------------------------------------------------- #include "boolean.h" #include //----------------------------------------------------------------------------- //----Forward declarations of classes for friend declarations template class linkedlist; template class linkedlistiterator; //----------------------------------------------------------------------------- //----Linked list node class //----------------------------------------------------------------------------- template class linkedlistnode { //----Friends //----The list and its iterator are friends of a node friend class linkedlist; friend class linkedlistiterator; //----Public parts public: //----Initial value constructor linkedlistnode(const NodeType& NewNodeData); //----Set and get node data void SetNodeData(const NodeType& NewNodeData); NodeType GetNodeData(void) const ; //----Private parts private: //----Node data NodeType Data; //----Link to next node linkedlistnode *NextNodePointer; linkedlistnode *PreviousNodePointer; }; //----------------------------------------------------------------------------- //----Linked list class //----------------------------------------------------------------------------- template class linkedlist { //----Friends //----The iterator is a friend of a list friend class linkedlistiterator; //----Public parts public: //----Default constructor linkedlist(void); //----Copy constructor linkedlist(const linkedlist& CopyThisList); //----Destructor ~linkedlist(void); //----Test if empty boolean Empty(void) const ; //----Get data from first and last node NodeType GetFirstNodeData(void) const ; NodeType GetLastNodeData(void) const ; //----Add node at start and end void AddAtStart(const NodeType& NewNodeData); void AddAtEnd(const NodeType& NewNodeData); //----Delete from the start and end of the list if possible void DeleteAtStart(void); void DeleteAtEnd(void); //----Dump for debugging void Dump(void) const ; //----Private parts private: //----Function to make new nodes and check linkedlistnode *MakeNewNode(const NodeType& NewNodeData) const ; //----Function to delete a node and update the links void DeleteOldNode(linkedlistnode *OldNodePointer); //----Pointer to first node linkedlistnode *FirstNodePointer; linkedlistnode *LastNodePointer; }; //----------------------------------------------------------------------------- //----Linked list (external) iterator class //----------------------------------------------------------------------------- template class linkedlistiterator { //----Public parts public: //----Constructor taking name of list linkedlistiterator(linkedlist& ListToIterate); //----Checks if current, next, and previous are there boolean NodeExists(void) const ; boolean NextNodeExists(void) const ; boolean PreviousNodeExists(void) const ; //----To the start, end, next, and previous node of the linked list void ToStart(void); void ToEnd(void); void operator ++(void); void operator --(void); //----Set and get current node's data void SetCurrentData(const NodeType& NewNodeData); NodeType CurrentData(void) const ; //----Add node before or after void AddBefore(const NodeType& NewNodeData) const ; void AddAfter(const NodeType& NewNodeData) const ; //----To delete here, previous, and next void DeleteCurrent(void); void DeleteNext(void); void DeletePrevious(void); //----Private parts private: //----The list being iterated linkedlist *TheList; //----The current node linkedlistnode *CurrentNodePointer; }; //----------------------------------------------------------------------------- #endif //-----------------------------------------------------------------------------