#include #include #include using namespace std; typedef int stack_type; class stack { public: stack(); bool empty(); void psh(stack_type); stack_type pop(); private: struct stacknode { stack_type item; struct stacknode*next; }; stacknode*top; }; stack::stack() { top = NULL; } bool stack::empty() { return !top; } void stack::psh(stack_type newitem) { stacknode*newnode; newnode = new stacknode; newnode->item = newitem; newnode->next = top; top = newnode; } stack_type stack::pop() { assert(top); int result = top->item; top = top->next; return result; } int main() { stack S=stack(); S.psh(4); S.psh(3); S.psh(6); cout << "pop() returns " << S.pop() << endl; S.psh(5); while(!S.empty()) cout << S.pop() << endl; return 0; }