// file stack.h, adapter of vector (dynamic array) to the stack interface. -bds 9/06 #ifndef __STACK_H #define __STACK_H #include "vector.h" template< typename T > struct stack: private vector { void push(const T& x) { vector::push_back(x); } void pop() { vector::pop_back(); } T& top() { return vector::end()[-1]; } // back() could be used. bool empty() { return vector::empty(); } }; #endif // __STACK_H // To test: copy to stack.cc, uncomment main(), compile, run. /* int main() { stack S; S.push(8); S.push(7); S.push(4); assert(! S.empty()); // Size is 3. assert(S.top() == 4); S.pop(); assert(S.top() == 7); S.pop(); assert(S.top() == 8); assert(! S.empty()); // Size is 1. S.pop(); assert(S.empty()); } */