 // Purpose.  Mediator
 //
 // Discussion.  On the left: Node objs  #include <iostream.h>
 // interact directly with each other,
 // recursion is required, removing a    class Node {
 // Node is hard, and it is not possi-   public:
 // ble to remove the first node.  On       Node( int v ) { val_ = v; }
 // the right: a "mediating" List class     int getVal()  { return val_; }
 // focuses and simplifies all the ad-   private:
 // ministrative responsibilities, and      int  val_;
 // the recursion (which does not scale  };
 // up well) has been eliminated.
                                         class List {
 #include <iostream.h>                   public:
                                            List() {
 class Node {                                  for (int i=0; i < 10; i++)
 public:                                          arr_[i] = 0;
    Node( int v, Node* n ) {                      num_ = 0;
       val_ = v;                            }
       next_ = n;                           void addNode( Node* n ) {
    }                                          arr_[num_++] = n;
    void traverse() {                       }
       cout << val_ << "  ";                void traverse() {
       if (next_)                              for (int i=0; i < num_; i++)
          next_->traverse();                      cout << arr_[i]->getVal()
       else                                          << "  ";
          cout << endl;                        cout << endl;
    }                                       }
    void removeNode( int v ) {              void removeNode( int v ) {
       Node*  ptr = (Node*) 1;                 int  i, j;
       removeNode_( v, &ptr );                 for (i=0; i < num_; i++)
    }                                             if (arr_[i]->getVal() == v)
 private:                                         {
    int    val_;                                     for (j=i; j < num_; j++)
    Node*  next_;                                       arr_[j] = arr_[j+1];
    void removeNode_(int v, Node** n) {              num_--;
       if (val_ == v)                                break;
          *n = next_;                             }
       else                                 }
       {                                 private:
          next_->removeNode_( v, n );       Node*  arr_[10];
          if (*n != (Node*) 1)              int    num_;
          {                              };
             next_ = *n;
             *n = (Node*) 1;             void main( void )
          }                              {
       }                                    List  lst;
    }                                       Node  one( 11 ),  two( 22 );
 };                                         Node  thr( 33 ),  fou( 44 );
                                            lst.addNode( &one );
 void main( void )                          lst.addNode( &two );
 {                                          lst.addNode( &thr );
    Node  fou( 44, 0 );                     lst.addNode( &fou );
    Node  thr( 33, &fou );                  lst.traverse();
    Node  two( 22, &thr );                  lst.removeNode( 44 );
    Node  one( 11, &two );                  lst.traverse();
    one.traverse();                         lst.removeNode( 11 );
    one.removeNode( 44 );                   lst.traverse();
    one.traverse();                      }
    one.removeNode( 22 );
    one.traverse();                      // 11  22  33  44
 }                                       // 11  22  33
                                         // 22  33
 // 11  22  33  44
 // 11  22  33
 // 11  33

