home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_03 / 8n03049a < prev    next >
Text File  |  1990-03-20  |  2KB  |  75 lines

  1. *****Listing 2*****
  2.  
  3.  
  4. // Node[T] - Implementation
  5. // Parametrize the file by T ...
  6. typedef int T;
  7.  
  8. typedef int Truth;
  9.  
  10. class Node {
  11. public:
  12.     Node(T x)            { Next = 0; val = x; }
  13.     Node *next()         { return Next; }
  14.     T value()            { return val; }
  15.     void link( Node *p)  { Next = p; }
  16. private:
  17.     Node *Next;
  18.     T val;
  19. };
  20.     
  21. class Sloop {
  22. public:
  23.     Sloop()          { before = after = 0; }
  24.     Truth isbegin()  { return (before == 0); }
  25.     Truth isend()    { return (after == 0); }
  26.     void next()     { move( &after, &before); }
  27.     void prev()     { move( &before, &after); }
  28.     T geta()         { return after->value(); }
  29.     T getb()         { return before->value(); }
  30.     Truth puta( T x) { return put( x, &after); }
  31.     Truth putb( T x) { return put( x, &before); }
  32.     T dela()         { return del( &after); }
  33.     T delb()         { return del( &before); }
  34. private:
  35.     Node *before,
  36.          *after;
  37.  
  38.     // these private functions are candidates for static 
  39.     // in C++ 2.0
  40.  
  41.     // move moves a node from the head of the 
  42.     // pre list to the head of the post list.
  43.     // pre is before point, post is after point
  44.     void move( Node **pre, Node **post)
  45.     {
  46.         if( (*pre) ) {
  47.             Node *t = (*pre);
  48.             (*pre) = (*pre)->next();
  49.             t->link((*post));
  50.             (*post) = t;
  51.         }
  52.     }
  53.  
  54.     Truth put( T x, Node **p)
  55.     {
  56.         Node *t;
  57.         if( t = new Node(x)) {
  58.             t->link( *p);
  59.             *p = t;
  60.         }
  61.         return (t != 0 );
  62.     }
  63.  
  64.     T del( Node **p)
  65.     {
  66.         Node *t = (*p);
  67.         (*p) = (*p)->next();
  68.         T r;
  69.         r = t->value();
  70.         delete t;
  71.         return r;
  72.     }
  73. };
  74.  
  75.