// A module of KPlib v1.4.2. // Written by Keith Pomakis during the summer of 1994. // Released to the public domain on October 10, 1994. // This version released on March 30, 1999. #ifndef KP_STACK_DEFINED #define KP_STACK_DEFINED #include "KPbasic.h" #include "KPList.h" #include // Assumes Element has a default constructor and operator=(). template class KPStack { public: KPStack(); KPStack(const KPStack &stack); KPStack(const KPList &list); KPStack(const Element &element); ~KPStack(); operator KPList() const; KPStack &operator=(const KPStack &stack); KPStack &operator=(const Element &element); KPStack &push(const Element &element); Element pop(); Element &top(); const Element &top() const; int size() const; bool is_empty() const; KPStack &clear(); protected: static void stack_empty_error(const char *fname); KPList my_list; }; /****************************************************************************/ template inline KPStack::KPStack(): my_list() { /* do nothing */ } /****************************************************************************/ template inline KPStack::KPStack(const KPStack &stack): my_list(stack.my_list) { /* do nothing */ } /****************************************************************************/ template inline KPStack::KPStack(const KPList &list): my_list(list) { /* do nothing */ } /****************************************************************************/ template inline KPStack::KPStack(const Element &element): my_list(element) { /* do nothing */ } /****************************************************************************/ template inline KPStack::~KPStack() { my_list.clear(); } /****************************************************************************/ template inline KPStack::operator KPList() const { return my_list; } /****************************************************************************/ template inline KPStack & KPStack::operator=(const KPStack &stack) { my_list = stack.my_list; return *this; } /****************************************************************************/ template inline KPStack & KPStack::operator=(const Element &element) { my_list = element; return *this; } /****************************************************************************/ template inline KPStack & KPStack::push(const Element &element) { my_list.add_to_head(element); return *this; } /****************************************************************************/ template inline Element KPStack::pop() { if (my_list.size() < 1) stack_empty_error("pop()"); Element element = my_list.head(); my_list.remove_head(); return element; } /****************************************************************************/ template inline Element & KPStack::top() { if (my_list.size() < 1) stack_empty_error("top()"); return my_list.head(); } /****************************************************************************/ template inline const Element & KPStack::top() const { if (my_list.size() < 1) stack_empty_error("top()"); return my_list.head(); } /****************************************************************************/ template inline int KPStack::size() const { return my_list.size(); } /****************************************************************************/ template inline bool KPStack::is_empty() const { return my_list.is_empty(); } /****************************************************************************/ template inline KPStack & KPStack::clear() { my_list.clear(); return *this; } /****************************************************************************/ template void KPStack::stack_empty_error(const char *fname) { cerr << "KPStack: " << fname << " - stack empty\n"; exit(EXIT_FAILURE); } /****************************************************************************/ #endif /* KP_STACK_DEFINED */