/* * This is a simple stack template class based on the standard list. */ #ifndef _lstack_h_ #define _lstack_h_ #include using namespace std; /* * This template class implements a standard stack. */ template class Stack { public: // Push an item onto the stack. void push(const T & item) { m_data.push_back(item); } // Pop an item from the stack. Return T() if the stack is empty. T pop() { if(m_data.empty()) { return T(); } else { T ret = m_data.back(); m_data.pop_back(); return ret; } } // An alternate pop which returns through the argument list when the // stack is non-empty (and returns true), but just returns false if // the stack is empty. This can also be more efficient if T is large, // and will work when T has no empty-argument constructor. bool pop(T &ret) { if(m_data.empty()) { return false; } else { ret = m_data.back(); m_data.pop_back(); return true; } } // Tell if the stack is empty bool empty() { return m_data.empty(); } // Number of items on the stack. int size() { return m_data.size(); } private: list m_data; }; #endif /* _lstack_h_ */