#ifndef FALSE #define FALSE 0 #endif #ifndef NULL #define NULL 0 #endif template class Gstack { public: virtual void push(T) = 0; // pure virtual function virtual T pop() = 0; // pure virtual function virtual bool isempty() = 0; // pure virtual function virtual bool isfull() = 0; // pure virtual function void reverse ( int); // a prototype only }; #include template class Array_stack : public Gstack { int sz; int top; T* astack; public: Array_stack(int Nitems ) { sz = Nitems; astack = new T[sz]; top = sz; } Array_stack( ) { sz = 100; astack = new T[sz]; top = sz; } ~Array_stack() { delete[] astack; } void push(T x) { assert( !isfull() ); astack[--top] = x; } T pop() { assert( !isempty() ); return( astack[top++] ); } bool isempty() { return( top == sz ); } bool isfull() { return( top == 0 ); } }; template class Link_stack : public Gstack { struct node { T data; node* next; node(T d, node* n) { data = d; next = n; } }; node* top; public: Link_stack( ) { top = NULL; } void push(T x) { top = new node(x, top); } T pop() { assert( !isempty() ); T t = top -> data; node* oldtop = top; top = top -> next; delete oldtop; return t; } bool isempty() { return( top == NULL ); } bool isfull() { return( FALSE ); } }; #include //#include "Vstack.h" // Doesn't know about Array_stack or Link_stack template void Gstack :: reverse( int n ) { int i; for( i = 0; i < n; i++ ) { this->push((T)('a'+i)); } for( i = 0; i < n; i++ ) { cout << this->pop() << ' '; } cout << endl; } //#include "Vstack.h" //#include "Linkedstack.h" //#include "Arraystack.h" int main() { Array_stack s1(500); Link_stack s2; // but not s2(); Array_stack s3; Link_stack s4; s1.reverse (20); s2.reverse (20); s3.reverse (20); //reverse knows about integers and chars s4.reverse (20); //as well as arrays and linked lists, member funct. return 0; } /* /////////////Code Warrior /////////// 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 t s r q p o n m l k j i h g f e d c b a t s r q p o n m l k j i h g f e d c b a /////////////g++ ////////////// 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 t s r q p o n m l k j i h g f e d c b a t s r q p o n m l k j i h g f e d c b a */