/////////////////////////////////////////////////////////////////////////
//       Answer  to C++ Problem 4 
//    compile the C++ programs with   g++   compiler on linux    
/////////////////////////////////////////////////////////////////////////
#include <iostream.h>

template <class T>
class MyStack { 
  int numelems ; 
  struct cell { 
     T            data ; 
     struct cell *next ; 
  } ; 
  cell *top ; 
  public: 
     MyStack() {
       top = NULL ; 
       numelems = 0 ; 
     }

     ~MyStack() {
        cell *t ; 

        for( ; top != NULL ; ) {
           t = top ; 
           top = top->next ; 
           delete t ; 
        }
     }
     
     int numelements() {
       return(numelems) ; 
     }

     void push(T item) {
       cell *t ;  
       
       t       = new cell ; 
       t->data = item ;
       t->next = top ; 
       top     = t ; 
       numelems++ ; 
     }

     T pop() {
        cell *t ; 
        T     data ; 

        t = top ; 
        top = top->next ; 
        data = t->data ; 
        delete t ; 
        numelems-- ; 
        return(data) ; 
     }
} ; 

main()
{
   MyStack<int> a ; 
   MyStack<char> b ; 
   int  i,n ; 

   for(i=0 ; i < 5 ; i++) {
      a.push(i) ; 
   }

   b.push('a') ; 
   b.push('b') ; 
   b.push('c') ; 

   cout << "== Stack a ===\n" ; 
   n = a.numelements() ; 
   for(i=0 ; i < n ; i++) {
     cout << a.pop() << endl ; 
   }
  
   cout << "== Stack b ===\n" ; 
   n = b.numelements() ; 
   for(i=0 ; i < n ; i++) {
     cout << b.pop() << endl ; 
   }
}
/////////////////////////////////////////////////////////////////////////
//       Answer  to the other C++ Problem 4 
/////////////////////////////////////////////////////////////////////////
#include <iostream.h>
#include <iostream.h>

template <class T>
class MyVector {
  int num_elems ; 
  T   *arr ;  
 
  public: 
    MyVector() {
      num_elems = 0 ; 
      arr  = NULL ; 
    }

    MyVector(int s) {
      num_elems = s ; 
      arr = new T[num_elems] ; 
    }

    ~MyVector() {
      if (num_elems) delete [] arr ; 
    }

    int size() {
      return(num_elems) ; 
    }

    void set_component(int indx,T item) {
      T *t ;
      int i ; 

      if (indx >= num_elems) {
        t = arr ; 
        arr = new T[indx+1]  ; 
        for(i=0 ; i < num_elems ; i++) 
          arr[i] = t[i] ; 
        num_elems = indx + 1 ; 
        delete [] t ; 
      }
      arr[indx] = item ; 
    }

    T get_component(int indx) {
       return(arr[indx]) ; 
    }
} ; 

main()
{
   int i ; 
   MyVector<char> u    ; 
   MyVector<int>  v(3) ; 
 
   cout << u.size() << " " << v.size() << endl ; 
   
   u.set_component(0,'a') ; 
   u.set_component(1,'b') ; 
   u.set_component(2,'c') ; 
      
   for(i=0 ; i < 5 ; i++) {
     v.set_component(i,i+1) ; 
   }

   for(i=0 ; i < u.size() ; i++) {
     cout << u.get_component(i)  ; 
   }
   cout << endl ; 

   for(i=0 ; i < v.size() ; i++) {
     cout << v.get_component(i) << " " ; 
   }
   cout << endl ; 
}