/* * File: stack.cpp * Member functions implementations for a Stack ADT * * From Dr. Mateen Rizki * Modified: spc * * CLASS INVARIANT * 1. If the stack is empty, the top of stack is undefined * 2. If the stack is non-empty, top_node holds address of top node of stack */ #include "stack-rizki.h" template stack::stack() { top_node = 0; } template void stack::push(const Stack_entry &item) { node *temp = new node; if (temp == NULL) throw string("bad allocation"); temp->data = item; temp->next = top_node; top_node = temp; } template void stack::pop() { if (empty()) throw string("stack underflow"); node *temp = top_node; top_node = top_node->next; delete temp; } template Stack_entry stack::top() const { if (empty()) throw string("stack underflow"); return top_node->data; } template bool stack::empty() const { return top_node == 0; } template class stack; template class stack;