Stack.cpp

//Stack #include <iostream> #include <string.h> using namespace std; class StackNode { private: char data; StackNode *next; public : StackNode(){ next=0; } StackNode(char n ,StackNode* ptr=0 ){ data =n ; next= ptr; } friend class Stack; }; class Stack { private: StackNode *head,*tail; public: Stack() { head=tail=0; } void push(char d){ head = new StackNode (d,head); if (tail ==0) tail = head ; } char pop (){ char d = head->data; StackNode * temp = head; if(head == tail ) head = tail = 0; else head = head->next; delete temp; return d; } bool isEmpty(){ if(head == 0) return true; else return false; } }; int main(int argc, char *argv[]) { Stack s; s.push('m'); s.push('o'); s.push('c'); s.push('l'); s.push('e'); s.push('W'); while(!s.isEmpty()) { cout << s.pop(); } cout << endl ; return 0; }