home *** CD-ROM | disk | FTP | other *** search
/ Microsoftware Monthly 19…2 Programming Power Tools / MASO9512.ISO / cpptutor / cpptutor.arj / EXAMPLES / EX1405.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-27  |  9.5 KB  |  305 lines

  1. // \EXAMPLES\EX1405.CPP
  2. //---------------------------------------------------------
  3. #include <iostream.h>
  4.  
  5. // forward declaration of ListItem class.
  6.  
  7. class ListItem;
  8.  
  9.  
  10. //----------------------------------------------------
  11. // CLASS List : A base class for the classes
  12. //              that implement specific kind of lists.
  13. //----------------------------------------------------
  14.  
  15. class List {
  16. public:
  17.   virtual void add(ListItem&) =0;
  18.   virtual int remove(ListItem&) =0;
  19.   virtual void display() =0;
  20. };
  21.  
  22. //----------------------------------------------------
  23. // CLASS ListItem : A class for nodes that are stored
  24. //                  in Lists.
  25. //----------------------------------------------------
  26.  
  27. class ListItem {
  28. private:
  29.   int value;
  30. public:
  31.   ListItem(int i = 0): value(i) { }
  32.   int operator==(const ListItem& li)
  33.   { return ( value == li.value); }
  34.   ListItem& operator=(const ListItem& li)
  35.   { value = li.value; return (*this); }
  36.   int get() { return value; }
  37. };
  38.  
  39.  
  40. //----------------------------------------------------
  41. // CLASS SLList : A class that implements a List as a
  42. //                singly-linked list.
  43. //----------------------------------------------------
  44.  
  45. class SLList : public List {
  46.   struct Node               // structure for nodes in the list
  47.   {
  48.     ListItem datum;         // value of current node
  49.     Node* next;             // pointer to next node
  50.   };
  51.   Node* pList;              // pointer to beginning of list
  52.   Node* last();             // return pointer to last node
  53. public:
  54.   SLList();                 // constructor
  55.   ~SLList();                // destructor
  56.   void add(ListItem&);      // add node to list
  57.   int remove(ListItem&);    // remove from list
  58.   void display();           // display contents of list
  59. };
  60.  
  61. //----------------------------------------------------
  62. // constructor for SLList
  63. //----------------------------------------------------
  64.  
  65. SLList::SLList() {
  66.   pList = NULL;
  67. }
  68.  
  69. //----------------------------------------------------
  70. // destructor for SLList
  71. //----------------------------------------------------
  72.  
  73. SLList::~SLList() {
  74.   Node* pTempNode;
  75.   while ( pList != NULL ) {      // delete all the nodes in list
  76.     pTempNode = pList;
  77.     pList = pList->next;
  78.     delete pTempNode;
  79.     }
  80. }
  81.  
  82. //----------------------------------------------------
  83. // last() private member function
  84. // It returns pointer to last node in list
  85. //----------------------------------------------------
  86.  
  87. SLList::Node* SLList::last() {
  88.   Node* currNode = pList;
  89.   Node* retNode = NULL;
  90.   while ( currNode != NULL ) {
  91.     retNode = currNode;
  92.     currNode = currNode->next;
  93.   }
  94.   return retNode;
  95. }
  96.  
  97. //----------------------------------------------------
  98. // add() - add a node to a list
  99. //----------------------------------------------------
  100.  
  101. void SLList::add(ListItem& s) {
  102.   Node* pNewNode = new Node;
  103.   if ( pNewNode == NULL )
  104.     cout << " Can't allocate list " << endl;
  105.   else {
  106.     Node* lastNode = last();
  107.     pNewNode->datum = s;           // assign data value
  108.     pNewNode->next = NULL;         //
  109.     if ( lastNode != NULL )
  110.       lastNode->next = pNewNode;   // last node in existing list
  111.                                    // points to new node
  112.     else
  113.       pList = pNewNode;            // this is first item in list
  114.     }
  115. }
  116.  
  117. //----------------------------------------------------
  118. // remove() remove a node from the list.  Any nodes that match
  119. //          the value of the argument are removed from the list.
  120. //----------------------------------------------------
  121.  
  122. int SLList::remove(ListItem& li) {
  123.   Node* currNode = pList;
  124.   Node* prevNode = NULL;
  125.   Node* nextNode;
  126.   int retval = 0;
  127.   while (currNode != NULL) {
  128.     nextNode = currNode->next;  // keep pointer to next node
  129.     if (currNode->datum == li)  // if datum matches, remove node
  130.     {
  131.       retval = 1;               // set return value
  132.       if ( prevNode == NULL )   // is this beginning of list?
  133.         pList = currNode->next; // List begins at next node
  134.      else                     // skip current node in link chain
  135.         prevNode->next = currNode->next;
  136.       delete currNode;          // delete current node
  137.     }
  138.     else
  139.       prevNode = currNode;      // advance previous node pointer
  140.     currNode = nextNode;        // advance current node pointer
  141.   }
  142.   return retval;
  143. }
  144.  
  145. //----------------------------------------------------
  146. // display() - print all of the nodes in the list
  147. //----------------------------------------------------
  148.  
  149. void SLList::display() {
  150.   Node* currNode = pList;
  151.   cout << "List contains:" << endl;
  152.   while ( currNode != NULL ) {
  153.     cout << currNode->datum.get() << endl;
  154.     currNode = currNode->next;
  155.   }
  156. }
  157.  
  158. //----------------------------------------------------
  159. // CLASS DLList : A class that implements a List as a
  160. //                doubly-linked list.
  161. //----------------------------------------------------
  162.  
  163. class DLList : public List {
  164.   struct Node            // structure for nodes in the list
  165.   {
  166.     ListItem datum;      // value of current node
  167.     Node* prev;          // pointer to previous node
  168.     Node* next;          // pointer to next node
  169.   };
  170.   Node* pList;           // pointer to beginning of linked list
  171.   Node* last();          // return pointer to last node in list
  172. public:
  173.   DLList();              // constructor
  174.   ~DLList();             // destructor
  175.   void add(ListItem&);   // add node to list
  176.   int remove(ListItem&); // remove nodes from list
  177.   void display();        // display contents of list
  178. };
  179.  
  180. //----------------------------------------------------
  181. // constructor for DLList
  182. //----------------------------------------------------
  183.  
  184. DLList::DLList() {
  185.   pList = NULL;
  186. }
  187.  
  188. //----------------------------------------------------
  189. // destructor for DLList
  190. //----------------------------------------------------
  191.  
  192. DLList::~DLList() {
  193.   Node* pTempNode;
  194.   while ( pList != NULL ) {   // delete all the nodes in the list
  195.     pTempNode = pList;
  196.     pList = pList->next;
  197.     delete pTempNode;
  198.     }
  199. }
  200.  
  201. //----------------------------------------------------
  202. // last() - private member function
  203. //          returns pointer to last node in list
  204. //----------------------------------------------------
  205.  
  206. DLList::Node* DLList::last() {
  207.   Node* currNode = pList;
  208.   Node* retNode = NULL;
  209.   while ( currNode != NULL ) {
  210.     retNode = currNode;
  211.     currNode = currNode->next;
  212.   }
  213.   return retNode;
  214. }
  215.  
  216. //----------------------------------------------------
  217. // add() - add a node to a list
  218. //----------------------------------------------------
  219.  
  220. void DLList::add(ListItem& s) {
  221.   Node* pNewNode = new Node;
  222.   if ( pNewNode == NULL )
  223.     cout << " Can't allocate list " << endl;
  224.   else {
  225.     Node* lastNode = last();
  226.     pNewNode->datum = s;           // assign data value
  227.     pNewNode->next = NULL;         //
  228.     if ( lastNode != NULL )  {
  229.       lastNode->next = pNewNode;   // last node in existing list
  230.                                    // points to new node
  231.       pNewNode->prev = lastNode;   // new node points back to
  232.                                    // last node in existing list
  233.     }
  234.     else {
  235.       pNewNode->prev = NULL;       // this is first item in list
  236.       pList = pNewNode;
  237.     }
  238.   }
  239. }
  240.  
  241. //----------------------------------------------------
  242. // remove() - remove nodes from the list.  Any ListItems
  243. //            that match the value of the argument are removed
  244. //----------------------------------------------------
  245.  
  246. int DLList::remove(ListItem& li) {
  247.   Node* currNode = pList;
  248.   Node* nextNode;
  249.   int retval = 0;
  250.   while (currNode != NULL) {
  251.     nextNode = currNode->next;      // keep pointer to next node
  252.     if (currNode->datum == li)      // if matches, remove node
  253.     {
  254.       retval = 1;                   // set return value
  255.       if ( currNode->prev == NULL ) // beginning of list?
  256.         pList = currNode->next;     // List begins at next node
  257.      else                      // skip current node in link chain
  258.         currNode->prev->next = currNode->next;
  259.       delete currNode;              // delete current node
  260.     }
  261.     currNode = nextNode;          // advance current node pointer
  262.   }
  263.   return retval;
  264. }
  265.  
  266. //---------------------------------------------------------
  267. // display() - print all of the nodes in the list
  268. //---------------------------------------------------------
  269.  
  270. void DLList::display() {
  271.   Node* currNode = pList;
  272.   cout << "List contains:" << endl;
  273.   while ( currNode != NULL ) {
  274.     cout << currNode->datum.get() << endl;
  275.     currNode = currNode->next;
  276.   }
  277. }
  278.  
  279. //----------------------------------------------------
  280. // FUNCTION useClass : client code to exercise List classes.
  281. // parameters:  List& lp  -  reference to List class object
  282. //----------------------------------------------------
  283.  
  284. void useClass(List& lp) {
  285.   for ( int i = 0; i < 10; i++ ) {  // put nodes in the list
  286.     ListItem li(i);                 // define a ListItem object
  287.     lp.add(li);                     // add this ListItem to list
  288.   }
  289.   lp.display();                     // display List
  290.   ListItem li(0);                   // add another node with
  291.   lp.add(li);                       // value = 0 to the list
  292.   lp.display();
  293.   lp.remove(li);             // remove both nodes with value = 0
  294.   lp.display();
  295. }
  296.  
  297. //----------------------------------------------------
  298. void main() {
  299.   SLList sl;
  300.   DLList dl;
  301.   useClass(sl);            // call client code with SLList object
  302.   useClass(dl);            // call client code with DLList object
  303. }
  304. //----------------------------------------------------
  305.