#include<iostream> using namespace std; template <class Object> class Stack{ private: struct Node{ Object data; Node * next; Node(const Object & theelem , Node * n = NULL) :data(theelem),next(n){} }; Node * topOfStack; int stacklen; public: Stack(Object & x) { Node * p = new Node(x); topOfStack = p; stacklen = 0; } void makeEmpty() { while(isEmpty()) { Node * p = new Node(); p = topOfStack; topOfStack = topOfStack -> next; delete p; } } bool isEmpty() const { if(NULL == topOfStack) return true; return false; } Object Top() const { return topOfStack -> data; } void Pop() { if(!isEmpty()) { Node * p = topOfStack; cout<<p->data<<" "; topOfStack = topOfStack -> next; delete p; stacklen--; } } void Push(const Object & x) { Node * p = new Node(x); p ->next = topOfStack; topOfStack = p; stacklen++; } ~Stack(){ Node * next ; while(topOfStack) { next = topOfStack ->next; delete topOfStack; topOfStack = next; } } }; int main() { int a = 1; Stack<int> dusk(a); for(int i = 0 ; i < 5 ; i++) { dusk.Push(i); } for(int i = 0 ; i < 5 ; i++) { dusk.Pop(); } }