home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_09 / 8n09119b < prev    next >
Text File  |  1990-07-09  |  2KB  |  52 lines

  1.  
  2.     class Node {
  3.  
  4.     public:
  5.              Node( T x)                    { val = x; Next = 0; }
  6.              Node *next()                  { return Next; }
  7.              void link( Node *neighbor)    { Next = neighbor; }
  8.              T value()                     { return val; }
  9.     private:
  10.              Node *Next;
  11.              T val;
  12.     };
  13.  
  14.         class Stack {
  15.         public:                 // these members are public
  16.                 Stack()                 { head = 0; }
  17.                 
  18.                 // precondition:  !isfull()
  19.                 void push( int x)       
  20.                 { 
  21.                     Node *p = new Node( x);
  22.                     if( head == 0)
  23.                         head = p;
  24.                     else {
  25.                         p->link( head);
  26.                         head = p;                  
  27.                     }
  28.                 }
  29.                 
  30.                 // precondition:  !isempty()
  31.                 int pop()               
  32.                 { 
  33.                     int r = head->value();
  34.                     Node *p = head;
  35.                     head = head->next();
  36.                     delete p;
  37.                     return r; 
  38.                 }
  39.                 
  40.                 Truth isempty()         { return head == 0; }
  41.                
  42.                 Truth isfull() 
  43.                 {
  44.                     Node *p = new Node( 0);
  45.                     delete p;
  46.                     return (p == 0);
  47.                 }
  48.         private:                // these (data) members are private
  49.                 Node *head;
  50.         };
  51.  
  52.