home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK4 / SAMPLES / CPPTUTOR / OOD / LIST.CP$ / LIST
Encoding:
Text File  |  1991-12-25  |  7.8 KB  |  339 lines

  1.  
  2. /********************************************************************
  3.  
  4.  FILE: LIST.CPP
  5.  
  6. ********************************************************************/
  7.  
  8. #include "list.h"
  9.  
  10. /********************************************************************
  11.  
  12.  ListElem::isEqual
  13.  
  14.  This function tests for equality against the specified element.
  15.  The default implementation is a comparison of the address.
  16.  
  17. ********************************************************************/
  18.  
  19. int ListElem::isEqual( ListElem *other )
  20. {
  21.     return( this == other );
  22. }
  23.  
  24.  
  25. /********************************************************************
  26.  
  27.  Implementation notes about List:
  28.  
  29.  A List is implemented as a doubly-linked ring of Nodes, each of
  30.  which contains a pointer to a ListElem. Each List contains an
  31.  endNode, which indicates the beginning and end point of the list,
  32.  and a current pointer, which indicates the current position in
  33.  the list. The initIterator function sets the current pointer to
  34.  the endNode, allowing the next call of getNext or getPrev to
  35.  return either the first or the last element in the list.
  36.  
  37. ********************************************************************/
  38.  
  39.  
  40. /********************************************************************
  41.  
  42.  List::List
  43.  
  44.  This function creates a new List by allocating the endNode.
  45.  
  46. ********************************************************************/
  47.  
  48. List::List()
  49. {
  50.     endNode = new Node;
  51.     endNode->next = endNode->prev = endNode;
  52.     endNode->data = 0;
  53.     current = 0;
  54. }
  55.  
  56. /********************************************************************
  57.  
  58.  List::addHead
  59.  
  60.  This function adds a new element to the head of the list.
  61.  
  62. ********************************************************************/
  63.  
  64. void List::addHead( ListElem *newHead )
  65. {
  66.     Node *newNode;
  67.  
  68.     newNode= new Node;
  69.     newNode->data = newHead;
  70.     newNode->prev = endNode;
  71.     newNode->next = endNode->next;
  72.     endNode->next = newNode;
  73.     newNode->next->prev = newNode;
  74. }
  75.  
  76. /********************************************************************
  77.  
  78.  List::addTail
  79.  
  80.  This function adds a new element to the tail of the list.
  81.  
  82. ********************************************************************/
  83.  
  84. void List::addTail( ListElem *newTail )
  85. {
  86.     Node *newNode;
  87.  
  88.     newNode = new Node;
  89.     newNode->data = newTail;
  90.     newNode->prev = endNode->prev;
  91.     newNode->next = endNode;
  92.     endNode->prev = newNode;
  93.     newNode->prev->next = newNode;
  94. }
  95.  
  96. /********************************************************************
  97.  
  98.  List::removeHead
  99.  
  100.  This function removes an element from the head of the list.
  101.  
  102. ********************************************************************/
  103.  
  104. ListElem *List::removeHead()
  105. {
  106.     Node *temp;
  107.     ListElem *removed;
  108.  
  109.     if( endNode->next != endNode )
  110.     {
  111.         temp = endNode->next;
  112.         endNode->next = temp->next;
  113.         temp->next->prev = endNode;
  114.  
  115.         removed = temp->data;
  116.         delete temp;
  117.         return removed;
  118.     }
  119.     else
  120.         return 0;
  121. }
  122.  
  123. /********************************************************************
  124.  
  125.  List::removeTail
  126.  
  127.  This function removes an element from the tail of the list.
  128.  
  129. ********************************************************************/
  130.  
  131. ListElem *List::removeTail()
  132. {
  133.     Node *temp;
  134.     ListElem *removed;
  135.  
  136.     if( endNode->prev != endNode )
  137.     {
  138.         temp = endNode->prev;
  139.         endNode->prev = temp->prev;
  140.         temp->prev->next = endNode;
  141.  
  142.         removed = temp->data;
  143.         delete temp;
  144.         return removed;
  145.     }
  146.     else
  147.         return 0;
  148. }
  149.  
  150. /********************************************************************
  151.  
  152.  List::getNext
  153.  
  154.  This function returns the next element in the list, assuming a current
  155.  position has been established. Returns 0 if at end of list.
  156.  
  157. ********************************************************************/
  158.  
  159. ListElem *List::getNext()
  160. {
  161.     if( !current )
  162.         return 0;
  163.  
  164.     current = current->next;
  165.     if( current == endNode )
  166.     {
  167.         current = 0;
  168.         return 0;
  169.     }
  170.     return current->data;
  171. }
  172.  
  173. /********************************************************************
  174.  
  175.  List::getPrev
  176.  
  177.  This function returns the previous element in the list, assuming a
  178.  current position has been established. Returns 0 if at beginning
  179.  of list.
  180.  
  181. ********************************************************************/
  182.  
  183. ListElem *List::getPrev()
  184. {
  185.     if( !current )
  186.         return 0;
  187.  
  188.     current = current->prev;
  189.     if( current == endNode )
  190.     {
  191.         current = 0;
  192.         return 0;
  193.     }
  194.     return current->data;
  195. }
  196.  
  197. /********************************************************************
  198.  
  199.  List::insertBefore
  200.  
  201.  This function inserts the specified element before the current
  202.  position in the list. It returns 1 if the insertion was successful,
  203.  or 0 if no current position was established.
  204.  
  205. ********************************************************************/
  206.  
  207. int List::insertBefore( ListElem *newElem )
  208. {
  209.  
  210.     // insert after current node
  211.     if( current )
  212.     {
  213.         Node *temp;
  214.  
  215.         temp->data = newElem;
  216.         temp->next = current;
  217.         temp->prev = current->prev;
  218.  
  219.         current->prev = temp;
  220.         temp->prev->next = temp;
  221.         return 1;
  222.     }
  223.     else
  224.         return 0;
  225. }
  226.  
  227. /********************************************************************
  228.  
  229.  List::insertAfter
  230.  
  231.  This function inserts the specified element after the current
  232.  position in the list. It returns 1 if the insertion was successful,
  233.  or 0 if no current position was established.
  234.  
  235. ********************************************************************/
  236.  
  237. int List::insertAfter( ListElem *newElem )
  238. {
  239.  
  240.     // insert after current node
  241.     if( current )
  242.     {
  243.         Node *temp;
  244.  
  245.         temp->data = newElem;
  246.         temp->prev = current;
  247.         temp->next = current->next;
  248.  
  249.         current->next = temp;
  250.         temp->next->prev = temp;
  251.         return 1;
  252.     }
  253.     else
  254.         return 0;
  255. }
  256.  
  257. /********************************************************************
  258.  
  259.  List:remove
  260.  
  261.  This function removes the current element from the list. It returns
  262.  the element if it was successfully removed, or 0 if no current
  263.  position was established.
  264.  
  265. ********************************************************************/
  266.  
  267. ListElem *List::remove()
  268. {
  269.     Node *temp;
  270.     ListElem *removed;
  271.  
  272.     if( current )
  273.     {
  274.         temp = current;   // hold element being removed
  275.  
  276.         current->prev->next = current->next;
  277.         current->next->prev = current->prev;
  278.  
  279.         current = current->next;
  280.  
  281.         removed = temp->data;
  282.         delete temp;
  283.         return removed;
  284.     }
  285.     else
  286.         return 0;
  287. }
  288.  
  289. /********************************************************************
  290.  
  291.  List::find
  292.  
  293.  This function searches the list for the first occurrence of the
  294.  specified element. It returns the place of the element in the
  295.  list if it was found, or 0 if it wasn't found.
  296.  
  297. ********************************************************************/
  298.  
  299. int List::find( ListElem *searchElem )
  300. {
  301.     Node *temp;
  302.     ListElem *temp2;
  303.     int i;
  304.  
  305.     temp = current;
  306.  
  307.     // Move to the head and search each element.
  308.     i = 1;
  309.     initIterator();
  310.     while( (temp2 = getNext()) != 0 )
  311.     {
  312.         if( temp2->isEqual( searchElem ) )
  313.             return i;
  314.         i++;
  315.     }
  316.  
  317.     // Not found, so restore current.
  318.     current = temp;
  319.     return 0;
  320. }
  321.  
  322. /********************************************************************
  323.  
  324.  List::~List
  325.  
  326.  This function removes all elements from the list, and deletes the
  327.  endNode.
  328.  
  329. ********************************************************************/
  330.  
  331. List::~List()
  332. {
  333.     while( removeTail() )
  334.         ;
  335.  
  336.     delete endNode;
  337. }
  338.  
  339.