home *** CD-ROM | disk | FTP | other *** search
/ Computer Panoráma / computer_panorama_1997-12-hibas.iso / SHARE / GRAPH / PTC051.ZIP / SRC / LIST.H < prev    next >
C/C++ Source or Header  |  1997-09-20  |  6KB  |  341 lines

  1. ////////////////////////
  2. // simple linked list //
  3. ////////////////////////
  4.  
  5. #ifndef __LIST_H
  6. #define __LIST_H
  7.  
  8. #include "misc.h"
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16. template <class T> class List 
  17. {
  18.     // friend class
  19.     friend class Iterator<T>;
  20.  
  21.     public:
  22.  
  23.         // setup
  24.         List();
  25.         ~List();
  26.         
  27.         // management
  28.         inline int add(T const &object);
  29.         inline int add(T *object);
  30.         //inline int insert(T *object,int position);
  31.         inline int replace(T *object,T *replacement);
  32.         inline int remove(T *object);
  33.         inline void remove();
  34.         inline int free(T *object);
  35.         inline void free();
  36.         
  37.         // list merger
  38.         inline void merge(List<T> &other);
  39.  
  40.         // node access
  41.         inline T* head() const;
  42.         inline T* tail() const;
  43.  
  44.         // information
  45.         inline int size() const;
  46.  
  47.         // operators
  48.         inline void operator =(List<T> &other);
  49.  
  50.     private:
  51.  
  52.         // node structure
  53.         struct NODE
  54.         {
  55.             T *object;
  56.             NODE *next;
  57.             NODE *prev;
  58.         };
  59.  
  60.         // nodes
  61.         NODE Root;
  62. };
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71. // iterator
  72. #include "iterator.h"
  73.  
  74.  
  75.         
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82. template <class T> List<T>::List()
  83. {
  84.     // initialize root node
  85.     Root.next=&Root;
  86.     Root.prev=&Root;
  87.     Root.object=NULL;
  88. }
  89.  
  90.  
  91. template <class T> List<T>::~List()
  92. {
  93.     // free list
  94.     free();
  95. }
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. template <class T> inline int List<T>::add(T const &object)
  106. {
  107.     // alloc object
  108.     T *store=new T;
  109.     if (!store) return 0;
  110.     
  111.     // assign
  112.     *store=object;
  113.     
  114.     // add to list
  115.     if (!add(store))
  116.     {
  117.         delete store;
  118.         return 0 ;
  119.     }
  120.     else return 1;
  121. }
  122.     
  123.     
  124. template <class T> inline int List<T>::add(T *object)
  125. {
  126.     // fail on null object
  127.     if (object==NULL) return 0;
  128.  
  129.     // create node
  130.     NODE *node=new NODE;
  131.     if (!node) return 0;
  132.     node->object=object;
  133.  
  134.     // add at tail
  135.     node->prev=Root.prev;
  136.     node->next=&Root;
  137.     Root.prev->next=node;
  138.     Root.prev=node;
  139.     return 1;
  140. }
  141.  
  142.  
  143. template <class T> inline int List<T>::replace(T *object,T *replacement)
  144. {
  145.     // seach through list for matching object
  146.     NODE *node=Root.next;
  147.     while (node->object)
  148.     {
  149.         // check for match
  150.         if (node->object==object)
  151.         {
  152.             // delete object
  153.             delete node->object;
  154.  
  155.             // replace it
  156.             node->object=replacement;
  157.             return 1;
  158.         }
  159.  
  160.         // move to next node
  161.         node=node->next;
  162.     }
  163.  
  164.     // no match
  165.     return 0;
  166. }
  167.  
  168.  
  169. template <class T> inline int List<T>::remove(T *object)
  170. {
  171.     // seach through list for matching object
  172.     NODE *node=Root.next;
  173.     while (node->object)
  174.     {
  175.         // check for match
  176.         if (node->object==object)
  177.         {
  178.             // remove from list
  179.             node->prev->next=node->next;
  180.             node->next->prev=node->prev;
  181.  
  182.             // delete node
  183.             delete node;
  184.             return 1;
  185.         }
  186.  
  187.         // move to next node
  188.         node=node->next;
  189.     }
  190.  
  191.     // no match
  192.     return 0;
  193. }
  194.  
  195.  
  196. template <class T> inline void List<T>::remove()
  197. {
  198.     // remove all list nodes
  199.     NODE *node=Root.next;
  200.     while (node->object)
  201.     {
  202.         NODE *old=node;
  203.         node=node->next;
  204.         delete old;
  205.     }
  206.  
  207.     // reset list
  208.     Root.next=&Root;
  209.     Root.prev=&Root;
  210. }
  211.  
  212.  
  213. template <class T> inline int List<T>::free(T *object)
  214. {
  215.     // remove the object from list
  216.     if (!remove(object)) return 0;
  217.  
  218.     // delete the object
  219.     delete object;
  220.     return 1;
  221. }
  222.  
  223.  
  224. template <class T> inline void List<T>::free()
  225. {
  226.     // free all list nodes
  227.     NODE *node=Root.next;
  228.     while (node->object)
  229.     {
  230.         NODE *old=node;
  231.         node=node->next;
  232.         delete old->object;
  233.         delete old;
  234.     }
  235.  
  236.     // clear list nodes
  237.     Root.next=&Root;
  238.     Root.prev=&Root;
  239. }
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248. template <class T> inline void List<T>::merge(List<T> &other)            // rename to add(list) ?
  249. {
  250.     // merge items another list with this list
  251.     Iterator<T> iterator(other);
  252.     T *object=iterator.current();
  253.     while (object)
  254.     {
  255.         add(object);
  256.         object=iterator.next();
  257.     }
  258.  
  259.     // remove all items from other list
  260.     other.remove();
  261. }
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269. template <class T> inline T* List<T>::head() const
  270. {                                     
  271.     // return head object
  272.     return Root.next->object;
  273. }
  274.  
  275.  
  276. template <class T> inline T* List<T>::tail() const
  277. {
  278.     // return tail object
  279.     return Root.prev->object;
  280. }
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288. template <class T> inline int List<T>::size() const
  289. {
  290.     // return the number of objects stored in the list
  291.     int count=0;
  292.     NODE *node=Root.next;
  293.     while (node->object)
  294.     {
  295.         node=node->next;
  296.         count++;
  297.     }
  298.     return count;
  299. }
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307. template <class T> inline void List<T>::operator =(List<T> &other)
  308. {
  309.     // free list
  310.     free();
  311.  
  312.     // add items from other list to this list
  313.     Iterator<T> iterator(other);
  314.     T *current=iterator.current();
  315.     while (current)
  316.     {
  317.         // copy to this list
  318.         T *copy=new T;
  319.         if (copy)
  320.         {
  321.             *copy=*current;
  322.             add(copy);
  323.         }
  324.  
  325.         // move to next item
  326.         current=iterator.next();
  327.     }
  328. }
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340. #endif
  341.