//Vstack.h//////////// #ifndef FALSE #define FALSE 0 #endif #ifndef NULL #define NULL 0 #endif #ifndef TONYSTACK #define TONYSTACK 1 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 }; #endif //AStack.h/////////// //#include "Vstack.h" #ifndef TONYASTACK #define TONYASTACK 1 template class Array_stack : public Gstack { int sz; int top; T* astack; public: Array_stack(int ); Array_stack(); ~Array_stack(); void push(T ); T pop(); bool isempty(); bool isfull(); }; #endif //LStack.h//////////// //#include "Vstack.h" #ifndef TONYLSTACK #define TONYLSTACK 1 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( ); void push(T ); T pop(); bool isempty() ; bool isfull() ; }; #endif //ArrayStack.cc////////// #include //#include "Astack.h" template Array_stack :: Array_stack (int Nitems) { sz = Nitems; astack = new T[sz]; top = sz; }; template Array_stack :: Array_stack () { sz = 100; astack = new T[sz]; top = sz; }; template Array_stack :: ~Array_stack () { delete[] astack; } template void Array_stack :: push (T x) { assert( !isfull() ); astack[--top] = x; } template T Array_stack :: pop() { assert( !isempty() ); return( astack[top++] ); } template bool Array_stack :: isempty() { return( top == sz ); } template bool Array_stack :: isfull () { return( top == 0 ); } //LinkStack.cc/////////// #include //#include "Lstack.h" template Link_stack :: Link_stack( ) { top = NULL; } template void Link_stack :: push(T x) { top = new node(x, top); } template T Link_stack :: pop() { assert( !isempty() ); T t = top -> data; node* oldtop = top; top = top -> next; delete oldtop; return t; } template bool Link_stack :: isempty() { return( top == NULL ); } template bool Link_stack :: isfull() { return( FALSE ); } //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(i); } for( i = 0; i < n; i++ ) { cout << s.pop() << ' '; } cout << endl; } // This is Main.cc //#include "Vstack.h" //#include "Astack.h" //#include "Lstack.h" //extern void reverse( Gstack &s, int n ); int main() { Array_stack s1(500); Link_stack s2; // but not s2(); // Array_stack s3; // Link_stack s4; 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; } /* 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 */ /* Main.cc:8: warning: return type for `main' changed to integer type Astack.h:18: warning: `class Array_stack' has virtual functions but non-virtual destructor Astack.h:18: warning: `class Array_stack' has virtual functions but non-virtual destructor Main.cc: In function `int main(...)': Main.cc:12: warning: unused variable `class Link_stack s4' Undefined first referenced symbol in file cout /var/tmp/cca000WA4.o __ls__7ostreamPFR7ostream_R7ostream /var/tmp/cca000WA4.o endl__FR7ostream /var/tmp/cca000WA4.o __ls__7ostreamc /var/tmp/cca000WA4.o __ls__7ostreami /var/tmp/cca000WA4.o ld: fatal: Symbol referencing errors. No output written to a.out */