home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c017 / 7.ddi / GPPLIB.ZIP / SLLIST.CC < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-17  |  4.1 KB  |  214 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of GNU CC.
  7.  
  8. GNU CC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY.  No author or distributor
  10. accepts responsibility to anyone for the consequences of using it
  11. or for whether it serves any particular purpose or works at all,
  12. unless he says so in writing.  Refer to the GNU CC General Public
  13. License for full details.
  14.  
  15. Everyone is granted permission to copy, modify and redistribute
  16. GNU CC, but only under the conditions described in the
  17. GNU CC General Public License.   A copy of this license is
  18. supposed to have been given to you along with GNU CC so you
  19. can know your rights and responsibilities.  It should be in a
  20. file named COPYING.  Among other things, the copyright notice
  21. and this notice must be preserved on all copies.  
  22. */
  23.  
  24. #include <stream.h>
  25. #include "<T>SLList.h"
  26.  
  27. // error handling
  28.  
  29.  
  30. void default_<T>SLList_error_handler(char* msg)
  31. {
  32.   cerr << "Fatal <T>SLList error. " << msg << "\n";
  33.   exit(1);
  34. }
  35.  
  36. one_arg_error_handler_t <T>SLList_error_handler = default_<T>SLList_error_handler;
  37.  
  38. one_arg_error_handler_t set_<T>SLList_error_handler(one_arg_error_handler_t f)
  39. {
  40.   one_arg_error_handler_t old = <T>SLList_error_handler;
  41.   <T>SLList_error_handler = f;
  42.   return old;
  43. }
  44.  
  45. void <T>SLList::error(const char* msg)
  46. {
  47.   (*<T>SLList_error_handler)(msg);
  48. }
  49.  
  50. <T>SLList::<T>SLList(<T>SLList& a)
  51. {
  52.   if (a.last == 0)
  53.     last = 0;
  54.   else
  55.   {
  56.     <T>SLListNode* p = a.last->tl;
  57.     <T>SLListNode* h = new <T>SLListNode(p->hd);
  58.     last = h;
  59.     p = p->tl;
  60.     for (;;)
  61.     {
  62.       <T>SLListNode* n = new <T>SLListNode(p->hd);
  63.       last->tl = n;
  64.       last = n;
  65.       if (p == a.last)
  66.       {
  67.         last->tl = h;
  68.         return;
  69.       }
  70.       else
  71.         p = p->tl;
  72.     }
  73.   }
  74. }
  75.  
  76. inline <T>SLList& <T>SLList::operator = (<T>SLList& a)
  77. {
  78.   if (last == a.last)
  79.     return *this;
  80.   else
  81.   {
  82.     clear();
  83.     if (a.last != 0)
  84.     {
  85.       <T>SLListNode* p = a.last->tl;
  86.       <T>SLListNode* h = new <T>SLListNode(p->hd);
  87.       last = h;
  88.       p = p->tl;
  89.       for (;;)
  90.       {
  91.         <T>SLListNode* n = new <T>SLListNode(p->hd);
  92.         last->tl = n;
  93.         last = n;
  94.         if (p == a.last)
  95.         {
  96.           last->tl = h;
  97.           return *this;
  98.         }
  99.         else
  100.           p = p->tl;
  101.       }
  102.     }
  103.   }
  104. }
  105. void <T>SLList::clear()
  106. {
  107.   if (last == 0)
  108.     return;
  109.  
  110.   <T>SLListNode* p = last->tl;
  111.   last->tl = 0;
  112.   last = 0;
  113.  
  114.   while (p != 0)
  115.   {
  116.     <T>SLListNode* nxt = p->tl;
  117.     delete(p);
  118.     p = nxt;
  119.   }
  120. }
  121.  
  122.  
  123. void <T>SLList::prepend(<T&> item)
  124. {
  125.   <T>SLListNode* t = new <T>SLListNode(item);
  126.   if (last == 0)
  127.     t->tl = last = t;
  128.   else
  129.   {
  130.     t->tl = last->tl;
  131.     last->tl = t;
  132.   }
  133. }
  134.  
  135. void <T>SLList::append(<T&> item)
  136. {
  137.   <T>SLListNode* t = new <T>SLListNode(item);
  138.   if (last == 0)
  139.     t->tl = last = t;
  140.   else
  141.   {
  142.     t->tl = last->tl;
  143.     last = last->tl = t;
  144.   }
  145. }
  146.  
  147.  
  148. void <T>SLListTrav::insert_after(<T&> item)
  149. {
  150.   <T>SLListNode* t = new <T>SLListNode(item);
  151.   if (L->last == 0)
  152.     t->tl = L->last = current = t;
  153.   else if (current == 0)
  154.   {
  155.     t->tl = L->last->tl;
  156.     L->last = t;
  157.   }
  158.   else if (L->last == current)
  159.   {
  160.     t->tl = current->tl;
  161.     current->tl = L->last = t;
  162.   }
  163.   else
  164.   {
  165.     t->tl = current->tl;
  166.     current->tl = t;
  167.   }
  168. }
  169.  
  170.  
  171. <T> <T>SLList::remove_front()
  172. {
  173.   if (last == 0)
  174.     error("remove_front of empty list");
  175.   <T>SLListNode* t = last->tl;
  176.   <T> res = t->hd;
  177.   if (t == last)
  178.     last = 0;
  179.   else
  180.     last->tl = t->tl;
  181.   delete t;
  182.   return res;
  183. }
  184.  
  185. int <T>SLList::remove_front(<T>& x)
  186. {
  187.   if (last == 0)
  188.     return 0;
  189.   else
  190.   {
  191.     <T>SLListNode* t = last->tl;
  192.     x = t->hd;
  193.     if (t == last)
  194.       last = 0;
  195.     else
  196.       last->tl = t->tl;
  197.     delete t;
  198.     return 1;
  199.   }
  200. }
  201.  
  202. void <T>SLList::del_front()
  203. {
  204.   if (last == 0)
  205.     error("del_front of empty list");
  206.   <T>SLListNode* t = last->tl;
  207.   if (t == last)
  208.     last = 0;
  209.   else
  210.     last->tl = t->tl;
  211.   delete t;
  212. }
  213.  
  214.