#ifndef FALSE #define FALSE 0 #endif #ifndef NULL #define NULL 0 #endif // This is Vstack.h 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 }; // This is Arraystack.h #include template class Array_stack : public Gstack { int sz; int top; T* astack; public: Array_stack(int Nitems = 100) { sz = Nitems; 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 ); } }; // This is Linkedstack.h 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( int Nitems = 100 ) { 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 ); } }; // This is reverse.cc #include //#include "Vstack.h" // Doesn't know about Arraystack or Linkedstack void reverse( Gstack &, int ); // a prototype void reverse( Gstack &s, int n ) { int i; for( i = 0; i < n; i++ ) { s.push('a'+i); } for( i = 0; i < n; i++ ) { cout << s.pop() << ' '; } cout << endl; } // This is main.cc //#include "Vstack.h" //#include "Linkedstack.h" //#include "Arraystack.h" //extern void reverse( stack &s, int n ); int main() { Array_stack s1(500); Link_stack s2; // or s2(137); but not s2(); Array_stack s3; Link_stack s4(50); reverse (s1, 20); reverse (s2, 20); // reverse (s3, 20); //problem reverse only knows about integers // reverse (s4, 20); //need to make reverse a member function, perhaps? return 0; } /* ////////////Code Warrior warns /////////// Warning : variable 'Nitems' is not used in function Stack.cc line 94 void main() Warning : variable 'Nitems' is not used in function Stack.cc line 94 void main() 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 */ /* ////////////////g++ says: ////////////// Stack.cc:95: warning: return type for `main' changed to integer type Stack.cc:38: warning: `class Array_stack' has virtual functions but non-virtual destructor Stack.cc:38: warning: `class Array_stack' has virtual functions but non-virtual destructor Stack.cc: In function `int main(...)': Stack.cc:99: warning: unused variable `class Link_stack s4' Stack.cc: In method `Link_stack::Link_stack(int)': Stack.cc:52: warning: unused parameter `int Nitems' Stack.cc: In method `Link_stack::Link_stack(int)': Stack.cc:52: warning: unused parameter `int Nitems' 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 */