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

  1. #include "CDList.h"
  2.  
  3. template <class T>
  4. CDListNode<T>::CDListNode(const T& _item)
  5. : item(_item) 
  6. {
  7. }
  8.  
  9. template <class T>
  10. CDListNode<T>::CDListNode(void)
  11. {
  12. }
  13.  
  14. template <class T>
  15. CDList<T>::CDList(void)
  16. {
  17.   head.prev = 0;
  18.   head.next = &tail;
  19.   tail.prev = &head;
  20.   tail.next = 0;
  21. }
  22.  
  23. template <class T>
  24. CDList<T>::~CDList()                              
  25.   destroy(); 
  26. }
  27.  
  28. template <class T>
  29. int 
  30. CDList<T>::isEmpty(void) const                
  31.   return head.next == &tail; 
  32. }
  33.  
  34. template <class T>
  35. CDList<T>::operator int() const                   
  36.   return !isEmpty(); 
  37. }
  38.  
  39. template <class T>
  40. T& 
  41. CDList<T>::getHead(void) const                 
  42.   return head.next->item; 
  43. }
  44.  
  45. template <class T>
  46. T& 
  47. CDList<T>::getTail(void) const                 
  48.   return tail.prev->item; 
  49. }
  50.  
  51. template <class T>
  52. CDList<T>& CDList<T>::operator+=(const T item)
  53. {
  54.   addTail(item);
  55.   return *this;
  56. }
  57.  
  58.  
  59. template <class T>
  60. CDList<T>::CDList(const CDList<T>& _list)
  61. {
  62.   head.prev = 0;
  63.   head.next = &tail;
  64.   tail.prev = &head;
  65.   tail.next = 0;
  66.  
  67.   CDListIterator<T> it(_list);
  68.   while (it)
  69.     addTail(it++);
  70. }
  71.  
  72. template <class T>
  73. CDList<T>& CDList<T>::operator=(const CDList<T>& _list)
  74. {
  75.   if (this != &_list)
  76.     {
  77.       destroy();
  78.       CDListIterator<T> it(_list);
  79.       while (it)
  80.         addTail(it++);
  81.     }
  82.   return *this;
  83. }
  84.  
  85. template <class T>
  86. void CDList<T>::addHead(const T item)
  87. {
  88.   CDListNode<T>* newNode = new CDListNode<T>(item);
  89.   newNode->next = head.next;
  90.   newNode->prev = &head;
  91.   head.next->prev = newNode;
  92.   head.next = newNode;
  93. }
  94.  
  95. template <class T>
  96. void CDList<T>::addTail(const T item)
  97. {
  98.   CDListNode<T>* newNode = new CDListNode<T>(item);
  99.   newNode->next = &tail;
  100.   newNode->prev = tail.prev;
  101.   tail.prev->next = newNode;
  102.   tail.prev = newNode;
  103. }
  104.  
  105. template <class T>
  106. T CDList<T>::remHead(void)
  107. {
  108.   CDListNode<T>* n = head.next;
  109.   head.next = n->next;
  110.   n->next->prev = &head;
  111.   T result = n->item;
  112.   delete n;
  113.   return result;
  114. }
  115.  
  116. template <class T>
  117. T CDList<T>::remTail(void)
  118. {
  119.   CDListNode<T>* n = tail.prev;
  120.   tail.prev = n->prev;
  121.   n->prev->next = &tail;
  122.   T result = n->item;
  123.   delete n;
  124.   return result;
  125. }
  126.  
  127. template <class T>
  128. void CDList<T>::destroy(void)
  129. {
  130.   CDListNode<T>* n = head.next;
  131.   while (n != &tail)
  132.     {
  133.       CDListNode<T>* w = n->next;
  134.       delete n;
  135.       n = w;
  136.     }
  137.   head.next = &tail;
  138.   tail.prev = &head;
  139. }
  140.  
  141. template <class T>
  142. int CDList<T>::count(void) const
  143. {
  144.   int c = 0;
  145.   CDListNode<T>* n = head.next;
  146.   while (n != &tail)
  147.     {
  148.       c++;
  149.       n = n->next;
  150.     }
  151.   return c;
  152. }
  153.  
  154. template <class T>
  155. CDListIterator<T>::CDListIterator(const CDList<T>& _list) 
  156. : list(&_list)          
  157.   cur = list->head.next; 
  158. }
  159.  
  160. template <class T>
  161. CDListIterator<T>::CDListIterator(const CDListIterator<T>& _it) 
  162. : list(_it.list)  
  163.   cur = _it.cur; 
  164. }
  165.  
  166. template <class T>
  167. CDListIterator<T>& 
  168. CDListIterator<T>::operator=(const CDListIterator<T>& _it)
  169. {
  170.   if (this != &_it)
  171.     {
  172.       list = _it.list;
  173.       cur = _it.cur;
  174.     }
  175.   return *this;
  176. }
  177.  
  178. template <class T>
  179. T& 
  180. CDListIterator<T>::current(void) const                                 
  181.   return cur->item; 
  182. }
  183.  
  184. template <class T>
  185. void 
  186. CDListIterator<T>::rewind(void)                                      
  187.   cur = list->head.next; 
  188. }
  189.  
  190. template <class T>
  191. void 
  192. CDListIterator<T>::fastforward(void)                                 
  193.   cur = list->tail.prev; 
  194. }
  195.  
  196. template <class T>
  197. CDListIterator<T>::operator int()                                         
  198.   return cur != &list->tail && cur != &list->head; 
  199. }
  200.  
  201. template <class T>
  202. T& 
  203. CDListIterator<T>::operator++(int)
  204. {
  205.   CDListNode<T>* n = cur;
  206.   cur = cur->next;
  207.   return n->item;
  208. }
  209.  
  210. template <class T>
  211. T& 
  212. CDListIterator<T>::operator++()
  213. {
  214.   cur = cur->next;
  215.   return cur->item;
  216. }
  217.  
  218. template <class T>
  219. T& 
  220. CDListIterator<T>::operator--(int)
  221. {
  222.   CDListNode<T>* n = cur;
  223.   cur = cur->prev;
  224.   return n->item;
  225. }
  226.  
  227. template <class T>
  228. T& 
  229. CDListIterator<T>::operator--()
  230. {
  231.   cur = cur->prev;
  232.   return cur->item;
  233. }
  234.  
  235. template <class T>
  236. void 
  237. CDListIterator<T>::insertAfter(const T item)
  238. {
  239.   CDListNode<T>* newNode = new CDListNode<T>(item);
  240.   newNode->next = cur->next;
  241.   newNode->prev = cur;
  242.   cur->next->prev = newNode;
  243.   cur->next = newNode;
  244.   cur = newNode;
  245. }
  246.  
  247. template <class T>
  248. void 
  249. CDListIterator<T>::insertBefore(const T item)
  250. {
  251.   CDListNode<T>* newNode = new CDListNode<T>(item);
  252.   newNode->next = cur;
  253.   newNode->prev = cur->prev;
  254.   cur->prev->next = newNode;
  255.   cur->prev = newNode;
  256.   cur = newNode;
  257. }
  258.  
  259. template <class T>
  260. void 
  261. CDListIterator<T>::remove(void)
  262. {
  263.   cur->next->prev = cur->prev;
  264.   cur->prev->next = cur->next;
  265.  
  266.   CDListNode<T>* next = cur->next;
  267.   delete cur;
  268.   cur = next;
  269. }
  270.  
  271.  
  272.