home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK4 / SAMPLES / CPPTUTOR / OOD / LIST.H$ / LIST
Encoding:
Text File  |  1991-11-06  |  4.1 KB  |  148 lines

  1. /********************************************************************
  2.  
  3.  FILE: LIST.H
  4.  
  5.  Defines List and its associated class, ListElem.
  6.  
  7. ********************************************************************/
  8.  
  9.  
  10. #if !defined( _LIST_H_ )
  11.  
  12. #define _LIST_H_
  13.  
  14. /********************************************************************
  15.  
  16.  ListElem
  17.  
  18.  Abstract base class for all objects to be stored in a List.
  19.  
  20.  Public Interface:
  21.  
  22.      isEqual - tests for equality against specified ListElem.
  23.          The default implementation is a comparison of the address;
  24.          this can be overriden in derived classes.
  25.  
  26. ********************************************************************/
  27.  
  28. class ListElem
  29. {
  30. public:
  31.     virtual int isEqual( ListElem *other );
  32.     virtual ~ListElem() {}
  33. };
  34.  
  35. /********************************************************************
  36.  
  37.  List
  38.  
  39.  Maintains a list to ListElem objects. Reference semantics are used;
  40.  that is, adding an element stores a pointer to that object instead
  41.  of making a copy of the object, and removing an object removes
  42.  the pointer but not the object itself. Operations supported are:
  43.  examining the element at either end of the list, adding an element
  44.  to either end of the list, removing the element at the end of the
  45.  list, searching for a given element, testing for an empty list,
  46.  iterating in either direction, removing the current element, and
  47.  inserting a new element before or after the current position.
  48.  
  49.  Public Interface:
  50.  
  51.      List - constructor.
  52.  
  53.      getHead, getTail - returns the element at the head or tail of
  54.          the list, but does not remove it.
  55.  
  56.      addHead, addTail - adds a new element to the head or tail of the
  57.          list.
  58.  
  59.      removeHead, removeTail - removes and returns the element at the
  60.          head or tail of the list.
  61.  
  62.      initIterator - initializes iterator. Must be called before
  63.          iteration in either direction begins. A subsequent call
  64.          to getNext returns the element at the head of the list,
  65.          whereas a call to getPrev returns the element at the the
  66.          tail of the list.
  67.  
  68.      getNext, getPrev - returns the next or previous element in the
  69.          list. A current position must have previously been
  70.          established through iteration, or with the find function.
  71.          Returns 0 if no further element.
  72.  
  73.      find - searches for the first occurence of the specified element
  74.          in the list. Returns 1 if the element was found, 0 if not.
  75.  
  76.      insertBefore, insertAfter - inserts a new element before or
  77.          after the current position. Returns 1 is insertion was
  78.          successful, 0 if current position hasn't been established.
  79.  
  80.      remove - removes and returns the current element from the list.
  81.          Returns 0 if current position hasn't been established.
  82.  
  83.      isEmpty - returns 1 is list is empty, 0 otherwise.
  84.  
  85. ********************************************************************/
  86.  
  87. struct Node
  88. {
  89. public:
  90.     ListElem *data;
  91.     Node     *next;
  92.     Node     *prev;
  93. };
  94.  
  95. class List
  96. {
  97. public:
  98.               List();
  99.               List( List& other );
  100.     ListElem *getHead() const;
  101.     ListElem *getTail() const;
  102.     void      addHead( ListElem *newItem );
  103.     void      addTail( ListElem *newItem );
  104.     ListElem *removeHead();
  105.     ListElem *removeTail();
  106.     void      initIterator();
  107.     ListElem *getNext();
  108.     ListElem *getPrev();
  109.     int       find( ListElem *searchItem );
  110.     int       insertBefore( ListElem *newItem );
  111.     int       insertAfter( ListElem *newItem );
  112.     ListElem *remove();
  113.     int       isEmpty() const;
  114.              ~List();
  115. private:
  116.     Node *endNode,
  117.          *current;
  118.  
  119. };
  120.  
  121. inline int List::isEmpty() const
  122. {
  123.     return ( endNode->next == endNode );
  124. }
  125.  
  126. inline ListElem *List::getHead() const
  127. {
  128.     if ( endNode->next )
  129.         return endNode->next->data;
  130.     return 0;
  131. }
  132.  
  133. inline ListElem *List::getTail() const
  134. {
  135.     if ( endNode->prev )
  136.         return endNode->prev->data;
  137.     return 0;
  138. }
  139.  
  140.  
  141. inline void List::initIterator()
  142. {
  143.     current = endNode;
  144. }
  145.  
  146. #endif // _LIST_H_
  147.  
  148.