home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_07_03 / v7n3081a.txt < prev    next >
Text File  |  1989-01-09  |  5KB  |  178 lines

  1. ---------------------------------------------------------------------------
  2.  
  3.                               Appendix P
  4. typedef Frog* T;
  5.  
  6. // Boolean type
  7. typedef int Truth;
  8.  
  9. // A node contains a value of type T.  Each node can link 
  10. // itself to another node
  11.  
  12. class Node {
  13. public:
  14.      Node( T x)                    { val = x; Next = 0; }
  15.      Node *next()                  { return Next; }
  16.      void link( Node *neighbor)    { Next = neighbor; }
  17.      T value()                     { return val; }
  18. private:
  19.      Node *Next;
  20.      T val;
  21. };
  22.  
  23. // A word about preconditions:  if the precondition is
  24. // not satisfied, the operation should not be performed.
  25. // If, nonetheless, the operation is performed, the results are
  26. // indeterminate ( e.g. spurious results, corruption of program,
  27. // memory fault ... )
  28.  
  29. // Lisp of T is a Lisp-style list of T values.
  30. // It is a simple abstraction, useful in its own right,
  31. // also useful for building more sophisticated list abstractions.
  32. // Note:  Lists with reference semantics are obtained by
  33. // substituting T* for T.
  34.  
  35. class Lisp {
  36. public:
  37.      Lisp()              { head = 0; }
  38.  
  39.      Truth isempty()     { return head == 0; }
  40.  
  41.      // lget returns the value at the head of the lisp
  42.      // without deleting it.  This is car in LISP.
  43.      // precondition:  !isempty() (lisp is not empty)
  44.      T lget()             { return head->value(); }
  45.  
  46.      // ltail returns the tail of the lisp.  cdr in LISP.
  47.      Lisp ltail()
  48.      {
  49.           Lisp r;
  50.           if( !isempty() )
  51.                r.head = head->next();
  52.           return r;
  53.      }
  54.  
  55.      // ladd inserts a new element with value x at the head
  56.      // of the lisp
  57.      void ladd(T x)
  58.      {
  59.           Node *p = new Node(x);  // error return of new should
  60.                                   // be checked ... 
  61.           if( !isempty() )        // member fun. calls member fun.
  62.                p->link( head);
  63.           head = p;
  64.      }
  65.      
  66.      // ldel deletes value at the head of the list
  67.      // and returns the value
  68.      // precondition:  !isempty() (lisp is not empty) 
  69.      T ldel()
  70.      {
  71.           Node *t = head;
  72.           T r     = head->value();
  73.           head    = head->next();
  74.           delete t;
  75.           return r; 
  76.      }
  77.  
  78. // some functions useful for building parametric List[T] with
  79. // random access to element on its index in the list
  80.  
  81.      // after inserts the value x after the first element in the
  82.      // lisp.  If the lisp is empty, x becomes first element
  83.      void after( T x)
  84.      {
  85.           Node *p = new Node(x);  // error return of new should
  86.                                   // be checked ... 
  87.           if( isempty() )
  88.                head = p;
  89.           else {
  90.                p->link( head->next() );
  91.                head->link( p);
  92.           }
  93.      }
  94.  
  95.      // pluck deletes the 2nd element in the lisp
  96.      // precondition:  lisp has at least 2 elements
  97.      // i.e. !tail().isempty()
  98.      T pluck()
  99.      {
  100.           Node *t = head->next();
  101.           T r     = t->value();
  102.           head->link( t->next());
  103.           delete t;
  104.           return r;
  105.      }
  106.  
  107. private:
  108.      Node *head;
  109. };
  110.  
  111. // Pond is a kind of Lisp wherein the individual elements can
  112. // be randomly accessed.  Element indices start at 0.
  113. // Note:  Pond inherits all of the properties of Lisp
  114.  
  115. class Pond : public Lisp {
  116. public:
  117.         Pond();
  118.  
  119.         Pond( Lisp &l);
  120.  
  121.      // length returns the number of elements in the list
  122.      int length();
  123.  
  124.      // get returns the nth value in the list
  125.      // precondition:  !isempty() && 0 <= n < length()
  126.      T get( int n);
  127.  
  128.      // del deletes the nth value in the list and returns it.
  129.      // precondition:  !isempty() && 0 <= n < length()  
  130.      T del( int n);
  131.  
  132.      // add adds x after element n
  133.      // precondition:  n >= 0
  134.      // Note:  if you want to insert to the head of the Pond,
  135.      // use the inherited void ladd( T x) member function     
  136.      void add( T x, int n);
  137.  
  138.      // tail returns the tail of the Pond.
  139.      Pond tail();
  140.  
  141.         // tail( n) returns the n-th tail of the Pond
  142.         Pond tail( int n);
  143. };
  144.  
  145. int Pond::length()
  146. {
  147.         int n;
  148.         Pond r;
  149.         
  150.         for( n = 0, r = *this; !r.isempty(); n++, r = r.tail() )
  151.                 ;
  152.         return n;
  153. }
  154.  
  155. T Pond::get( int n)             { return tail( n).lget(); }
  156.  
  157. T Pond::del( int n)             
  158.      return n ? tail( n - 1).pluck() : ldel(); 
  159. }
  160.  
  161. void Pond::add( T x, int n)     { tail( n).after( x); }
  162.  
  163. Pond::Pond() : ()               {}
  164.  
  165. Pond::Pond( Lisp &l)            { (*(Lisp *)this) = l; }
  166.  
  167. Pond Pond::tail()               { return Pond( ltail()); }
  168.  
  169. Pond Pond::tail( int n)
  170. {
  171.         for( Pond r = *this; !r.isempty() && (n-- > 0); r = r.tail() )
  172.                 ;
  173.         return r;
  174. }
  175.  
  176. ---------------------------------------------------------------------------
  177.