![]() |
| | #1 |
| Registered User Join Date: Dec 2001
Posts: 38
| List class Here is the main: #include <iostream.h> #include "list.h" void main() { List<int,int> s,*p; int i; for (i=1;i<=9;i++) p->addNode(i); p->traverse(); } here is the header file: #include <iostream.h> // ************************************************** ****************** // Declaration of a node // ************************************************** ****************** template <class TYPE> struct NODE { TYPE data; NODE *link; }; // ************************************************** ******************* // Declaration of list class // ************************************************** ******************* template <class TYPE, class KTYPE> class List { private: NODE<TYPE> *head; // pointer to the first node NODE<TYPE> *pos; // pointer to the current node NODE<TYPE> *rear; // pointer to the last node int count; // the number of nodes bool _insert(NODE<TYPE> *pPre, const TYPE &dataIn); // insert a new node after node *pPre void _delete(NODE<TYPE> *pPre, NODE<TYPE> *&pLoc, TYPE &dataOut); // delete node *pLoc, dataOut = pLoc->data bool _search(NODE<TYPE> *&pPre, NODE<TYPE> *&pLoc, const KTYPE &key); // search for the node containing a value // equal to key public: List(void); // Constructor for creating an empty list ~List(void); // Destructor int addNode(const TYPE &dataIn); // insert a new node bool removeNode(const KTYPE &key, TYPE &dataOut); // remove the node storing key with data of // the node returned through dataOut bool retrieveNode(const KTYPE &key, TYPE &dataOut); // retrieve the data at the node having key bool getNext (int fromWhere, TYPE &dataOut); // return the data from the next node bool emptyList(void){ return ( count == 0 ); } // test if the list is empty bool fullList(void); // test to see if the list is full; that is, // no more memory to add an additional node int listCount(void) { return count; } // return the number of nodes void traverse(void); // traverse list displaying keys }; // ************************************************** ***************** // The following are private member function definitions. // ************************************************** ***************** template <class TYPE, class KTYPE> bool List<TYPE, KTYPE>::_insert(NODE<TYPE> *pPre, const TYPE &dataIn) // ------------------------------------------------------------------- // Purpose: This function inserts a new node either at the beginning // or after the node *pPre. // ------------------------------------------------------------------- { // Create a new node with pNew pointing to the object. NODE<TYPE> *pNew; if (!(pNew = new NODE<TYPE>)) return false; pNew->data = dataIn; if (pPre == NULL) // empty list { pNew->link = head; head = pNew; } else // non-empty list { pNew->link = pPre->link; pPre->link = pNew; } if (pNew->link == NULL) rear = pNew; ++count; return true; } template <class TYPE, class KTYPE> void List<TYPE, KTYPE>::_delete(NODE<TYPE> *pPre, NODE<TYPE> *&pLoc, TYPE &dataOut) // ------------------------------------------------------------------- // Purpose: This function deletes node *pLoc // ------------------------------------------------------------------- { // Return the data value of the node to be deleted. dataOut = pLoc->data; if (pPre == NULL) // delete the first node head = pLoc->link; else pPre->link = pLoc->link; if (pLoc->link == NULL) rear = pPre; delete pLoc; --count; return; } template <class TYPE, class KTYPE> bool List<TYPE, KTYPE>::_search (NODE<TYPE> *&pPre, NODE<TYPE> *&pLoc, const KTYPE &key ) // ------------------------------------------------------------------ // Purpose: Search for the existence of a node containing a value // equal to key. // ------------------------------------------------------------------ { // Initialize pointers pPre and pLoc. pPre = NULL; pLoc = head; if (count == 0) return false; // Continue to loop until a target either matches key, the target is less // than key, or the end of the list is encountered. if (key > rear->data.key) { pPre = rear; return false; } while (key > pLoc->data.key) { pPre = pLoc; pLoc = pLoc->link; } if (key == pLoc->data.key) return true; return false; } // ************************************************** ***************** // The following are public member function definitions. // ************************************************** ***************** template <class TYPE, class KTYPE> List<TYPE, KTYPE>::List(void) // ------------------------------------------------------------------- // Purpose: This constructor initializes the member variables // head, pos and count. // ------------------------------------------------------------------- { head = NULL; pos = NULL; rear = NULL; count = 0; } template <class TYPE, class KTYPE> List<TYPE, KTYPE>::~List(void) // ------------------------------------------------------------------- // Purpose: This destructor removes all unwanted storage of nodes // before the object of class List ceases to exist. // ------------------------------------------------------------------- { NODE<TYPE> *deletePtr; // local pointer while( head != NULL ) // Continue to delete nodes until the list is empty { deletePtr = head; head = head->link; delete deletePtr; } count = 0; } template <class TYPE, class KTYPE> int List<TYPE, KTYPE>::addNode(const TYPE &dataIn) // ---------------------------------------------------------------- // Purpose: Public function for inserting a new node. // ---------------------------------------------------------------- { bool found; bool success; NODE<TYPE> *pPre; NODE<TYPE> *pLoc; // Search the position where a new node is to be inserted. found = _search(pPre, pLoc, dataIn.key); if(found) // Duplicate key exist. return (+1); // attempt to insert a new node within the linked list. success = _insert(pPre, dataIn); if (!success) return (-1); return (0); } template <class TYPE, class KTYPE> bool List<TYPE, KTYPE>::removeNode(const KTYPE &key, TYPE &dataOut) // ------------------------------------------------------------------ // Purpose: Public function for removing a node from within a // linked list with data of the node returned through // dataOut. // ------------------------------------------------------------------ { bool found; NODE<TYPE> *pPre; NODE<TYPE> *pLoc; // Search for a node containing the value of key. found = _search(pPre, pLoc, key); if(found) _delete(pPre, pLoc, dataOut); return found; } template <class TYPE, class KTYPE> bool List<TYPE, KTYPE>::retrieveNode(const KTYPE &key, TYPE &dataOut) // ------------------------------------------------------------------ // Purpose: This function retrieves the data at a node having key // ------------------------------------------------------------------ { bool found; NODE<TYPE> *pPre; NODE<TYPE> *pLoc; found = _search(pPre, pLoc, key); if (found) dataOut = pLoc->data; return found; } template <class TYPE, class KTYPE> bool List<TYPE, KTYPE>::getNext (int fromWhere, TYPE &dataOut) // -------------------------------------------------------------------- // Purpose: This public function returns the data from the next node // in the linked list. // -------------------------------------------------------------------- { // Test if fromWhere is at the beginning of the linked list. if(fromWhere == 0) { if(count == 0) return false; else { pos = head; dataOut = pos->data; return true; } } else // Continue from the current position { if(pos->link == NULL) return false; else { pos = pos->link; dataOut = pos->data; return true; } } } template <class TYPE, class KTYPE> bool List<TYPE, KTYPE>::fullList() // ------------------------------------------------------------------ // Purpose: Provides a test to see if the list is full; that is, // no further memory exist to add an additional node. // ------------------------------------------------------------------ { NODE<TYPE>* pNew; if (pNew = new NODE<TYPE>) // successful memory allocation { delete pNew; return false; } return true; } template <class TYPE, class KTYPE> void List<TYPE, KTYPE>::traverse(void) // ----------------------------------------------------------------- // Purpose: Traverses the linked list displaying the value of // the key from each node. // ----------------------------------------------------------------- { NODE<TYPE> *pNext = head; // Display the keys from each node of the linked list. while(pNext) { cout << pNext->data.key << " | "; pNext = pNext->link; } cout << endl; }
__________________ SilasP |
| SilasP is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help with FIFO QUEUE | jackfraust | C++ Programming | 23 | 04-03-2009 08:17 AM |
| Linked list of a class object?.... | chadsxe | C++ Programming | 6 | 12-08-2005 03:15 PM |
| Please Help - Problem with Compilers | toonlover | C++ Programming | 5 | 07-23-2005 10:03 AM |
| Problem with linked list ADT and incomplete structure | prawntoast | C Programming | 1 | 04-30-2005 01:29 AM |
| link list | Unregistered | C++ Programming | 4 | 12-13-2001 05:41 AM |