Turbo C++ Code For Stack
Turbo C++ Code For Stack
h> class stack { private: int s[10]; int top; public: stack() { top= -1; } void push(int n) { if(top==9) { cout<<"Stack overflow..."; return; } top= top + 1; s[top]= n; } int pop() { int data; if(top == -1) { cout<<"Stack underflow..."; return NULL; } data= s[top]; top=top-1; return data; } void print() { if(top== -1) { cout<<"Stack is empty..."; return; } else for (int i=top+1; i>=0; i--) cout<<s[i]<<endl; } }; void main() { int val, option; stack obj; while (option!=3) { Page 1
STACK clrscr(); cout<<"Press 1: for Push"<<endl <<"Press 2: for Pop" <<endl <<"Press 3: for exit"<<endl; cin>>option; switch (option) { case 1: cout<<"Enter value to insert"; cin>>val; obj.push(val); cout<<"Stack after insertion: "; obj.print(); break; case 2: cout<<"Value " <<obj.pop()<<" is deleted "; cout<<"Stack after deletion: "; obj.print(); break; } getch(); } }
Page 2