#include using namespace std; class Node { friend ostream &operator<< (ostream&, const Node*); public: int data; Node* next; }; ostream &operator<< (ostream& output, const Node* cur) { output << " [ " << cur->data << " ] -> "; if (cur->next == NULL) output << "||"; return output; } class Stack { friend ostream &operator<< (ostream&, const Stack&); public: Stack(void); ~Stack(); void Push(int); void Pop(int&, int&); int IsEmpty(void); private: Node* head; }; int Stack::IsEmpty(void) { return (head == NULL); } Stack::Stack(void) { head = NULL; } Stack::~Stack() { Node* t; while (head != NULL) { t = head; head = head->next; delete t; } } void Stack::Push(int i) { Node* t; t = new Node; t->data = i; t->next = head; head = t; } void Stack::Pop(int &rv, int &err) { Node* t; if (IsEmpty()) { err = 1; rv = 0; } else { err = 0; rv = head->data; t = head; head = head->next; delete t; } } ostream &operator<< (ostream& output, const Stack& c) { Node* t; t = c.head; while ( t != NULL) { output << t; t = t->next; } output << endl; return output; } void main (void) { Stack st; int rv, err; st.Push(42); cout << st; st.Push(31); cout << st; st.Push(59); cout << st; st.Pop(rv, err); cout << st; if (!err) cout << rv << endl; st.Pop(rv, err); cout << st; if (!err) cout << rv << endl; else cout << "ERROR" << endl; st.Pop(rv, err); cout << st; if (!err) cout << rv << endl; else cout << "ERROR" << endl; st.Pop(rv, err); cout << st; if (!err) cout << rv << endl; else cout << "ERROR" << endl; }