#ifndef STACKH #define STACKH #include //iostream includes cstddef, which defines size_t #include using namespace std; template class stack; template bool operator==(const stack& s1, const stack& s2); template //Don't write the = 100 anywhere else. class stack { public: typedef T value_type; private: T a[MAX_SIZE]; size_t n; //stack pointer: subscript of next free element public: stack(): n(0) {} ~stack(); void push(const T& t); T& pop(); friend bool operator==(const stack& s1, const stack& s2); }; template stack::~stack() { if (n != 0) { cerr << "Warning: stack still contains " << n << " value(s).\n"; } } //Push a value onto the stack. template void stack::push(const T& t) { if (n == MAX_SIZE) { //overflow cerr << "Can't push when size " << n << " == capacity " << MAX_SIZE << ".\n"; exit(EXIT_FAILURE); } a[n++] = t; } //Pop a value off the stack. template T& stack::pop() { if (n == 0) { //underflow cerr << "Can't pop when size " << n << " <= 0.\n"; exit(EXIT_FAILURE); } return a[--n]; } template bool operator==(const ::stack& s1, const ::stack& s2) { if (s1.n != s2.n) { return false; } for (size_t i = 0; i < s1.n; ++i) { if (s1.a[i] != s2.a[i]) { return false; } } return true; } template inline bool operator!=(const ::stack& s1, const ::stack& s2) { return !(s1 == s2); } #endif