home *** CD-ROM | disk | FTP | other *** search
/ Informática Multimedia: Special Games / INFESPGAMES.mdf / os2 / ribble / support / cslist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-16  |  3.7 KB  |  233 lines

  1. #include <CSList.h>
  2.  
  3. template <class T>
  4. CSListNode<T>::CSListNode(const T& _item) 
  5. : item(_item) 
  6. }
  7.  
  8. template <class T>
  9. CSList<T>::CSList(const CSList<T>& from)
  10. {
  11.   tail = 0;
  12.  
  13.   CSListIterator<T> it(from);
  14.  
  15.   while (it)
  16.     addTail(it++);
  17. }
  18.  
  19. template <class T>
  20. CSList<T>::CSList()                                             
  21.   tail = 0;  
  22. }
  23.  
  24. template <class T>
  25. CSList<T>::~CSList()                                    
  26.   destroy();  
  27. }
  28.  
  29. template <class T>
  30. CSList<T>& 
  31. CSList<T>::operator+=(const T item)
  32. {
  33.   addTail(item);
  34.   return *this;
  35. }
  36.  
  37. template <class T>
  38. T& 
  39. CSList<T>::getHead(void) const                       
  40.   return tail->next->item;  
  41. }
  42.  
  43. template <class T>
  44. T& 
  45. CSList<T>::getTail(void) const                       
  46.   return tail->item;  
  47. }
  48.  
  49. template <class T>
  50. int 
  51. CSList<T>::isEmpty(void) const                      
  52.   return (tail == 0);  
  53. }
  54.  
  55. template <class T>
  56. CSList<T>::operator int() const                         
  57.   return !isEmpty();  
  58. }
  59.  
  60. template <class T>
  61. CSList<T>& 
  62. CSList<T>::operator=(const CSList<T>& from)
  63. {
  64.   if (this != &from)
  65.     {
  66.       destroy();
  67.       tail = 0;
  68.  
  69.       CSListIterator<T> it(from);
  70.  
  71.       while (it)
  72.         addTail(it++);
  73.     }
  74.   return *this;
  75. }
  76.  
  77. template <class T>
  78. void 
  79. CSList<T>::destroy(void)
  80. {
  81.   CSListNode<T>* walk = tail;
  82.  
  83.   if (walk)
  84.     {
  85.       do
  86.         {
  87.           CSListNode<T>* n = walk->next;
  88.           delete walk;
  89.           walk = n;
  90.         } while (walk != tail);
  91.     }
  92.   tail = 0;
  93. }
  94.  
  95. template <class T>
  96. void 
  97. CSList<T>::addTail(const T item)
  98. {
  99.   CSListNode<T>* newNode = new CSListNode<T>(item);
  100.   if (tail)
  101.     {
  102.       newNode->next = tail->next;
  103.       tail = tail->next = newNode;
  104.     }
  105.   else
  106.     tail = newNode->next = newNode;
  107. }
  108.  
  109. template <class T>
  110. void 
  111. CSList<T>::addHead(const T item)
  112. {
  113.   CSListNode<T>* newNode = new CSListNode<T>(item);
  114.   if (tail)
  115.     {
  116.       newNode->next = tail->next;
  117.       tail->next = newNode;
  118.     }
  119.   else
  120.     tail = newNode->next = newNode;
  121. }
  122.  
  123. template <class T>
  124. CSList<T>::remHead(void)
  125. {
  126.   CSListNode<T>* n = tail->next;
  127.   if (n == tail)
  128.     tail = 0;
  129.   else
  130.     tail->next = n->next;
  131.  
  132.   T result = n->item;
  133.   delete n;
  134.   return result;
  135. }
  136.  
  137. template <class T>
  138. int 
  139. CSList<T>::count(void) const
  140. {
  141.   int c = 0;
  142.   if (tail)
  143.     {
  144.       CSListNode<T>* n = tail->next;
  145.       do
  146.         {
  147.           c++;
  148.           n = n->next;
  149.         }
  150.       while (n != tail->next);
  151.     }
  152.   return c;
  153. }
  154.  
  155. template <class T>
  156. CSListIterator<T>::CSListIterator(const CSList<T>& _list) 
  157. : list(&_list)         
  158.   cur = list->tail ? list->tail->next : 0; 
  159. }
  160.  
  161. template <class T>
  162. CSListIterator<T>::CSListIterator(const CSListIterator<T>& _it) 
  163. : list(_it.list) 
  164.   cur = _it.cur; 
  165. }
  166.  
  167. template <class T>
  168. T& 
  169. CSListIterator<T>::current(void) const                                
  170.   return cur->item;  
  171. }
  172.  
  173. template <class T>
  174. void 
  175. CSListIterator<T>::rewind(void)                                     
  176.   cur = list->tail ? list->tail->next : 0;  
  177. }
  178.  
  179. template <class T>
  180. CSListIterator<T>::operator int()                                        
  181.   return cur != 0;  
  182. }
  183.  
  184. template <class T>
  185. T& 
  186. CSListIterator<T>::operator++(int)
  187. {
  188.   CSListNode<T>* n = cur;
  189.   if (cur == list->tail)
  190.     cur = 0;
  191.   else
  192.     cur = cur->next;
  193.   return n->item;
  194. }
  195.  
  196. template <class T>
  197. T& 
  198. CSListIterator<T>::operator++()
  199. {
  200.   if (cur == list->tail)
  201.     cur = 0;
  202.   else
  203.     cur = cur->next;
  204.   return cur->item;
  205. }
  206.  
  207. template <class T>
  208. CSListIterator<T>& 
  209. CSListIterator<T>::operator=(const CSListIterator<T>& _it)
  210. {
  211.   if (this != &_it)
  212.     {
  213.       list = _it.list;
  214.       cur = _it.cur;
  215.     }
  216.   return *this;
  217. }
  218.  
  219.  
  220.