DS (RM) Program - Stack And Queue: Array Implementation
Output:
1.push 2.pop 3.display 4.exit
Enter your choice1
enter the element12
Inserted12
1.push 2.pop 3.display 4.exit
Enter your choice1
enter the element22
Inserted22
1.push 2.pop 3.display 4.exit
Enter your choice1
enter the element33
Inserted33
1.push 2.pop 3.display 4.exit
Enter your choice1
enter the element55
Inserted55
1.push 2.pop 3.display 4.exit
Enter your choice3
55 33 22 12
1.push 2.pop 3.display 4.exit
Enter your choice 2
Deleted55
1.push 2.pop 3.display 4.exit
Enter your choice2
Deleted33
1.push 2.pop 3.display 4.exit
Enter your choice2
Deleted22
1.push 2.pop 3.display 4.exit
Enter your choice2
Deleted12
1.push 2.pop 3.display 4.exit
Enter your choice 2
Stack Underflow
1.push 2.pop 3.display 4.exit
Enter your choice 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | #include<iostream> #include<conio.h> #include<stdlib.h> class stack { int stk[5]; int top; public: stack() { top=-1; } void push(int x) { if(top > 4) { cout <<"Stack Overflow"; return; } stk[++top]=x; cout <<"Inserted" <<x; } void pop() { if(top <0) { cout <<"Stack Underflow"; return; } cout <<"Deleted" <<stk[top--]; } void display() { if(top<0) { cout <<"Stack Empty"; return; } for(int i=top;i>=0;i--) cout <<stk[i] <<" "; } }; void main() { clrscr(); int ch; stack st; while(1) { cout <<"\n1.push\t 2.pop\t 3.display\t 4.exit\nEnter your choice"; cin >> ch; switch(ch) { case 1: cout <<"enter the element"; cin >> ch; st.push(ch); break; case 2: st.pop(); break; case 3: st.display(); break; case 4: exit(0); } } getch(); } |
Output:
1.push 2.pop 3.display 4.exit
Enter your choice1
enter the element12
Inserted12
1.push 2.pop 3.display 4.exit
Enter your choice1
enter the element22
Inserted22
1.push 2.pop 3.display 4.exit
Enter your choice1
enter the element33
Inserted33
1.push 2.pop 3.display 4.exit
Enter your choice1
enter the element55
Inserted55
1.push 2.pop 3.display 4.exit
Enter your choice3
55 33 22 12
1.push 2.pop 3.display 4.exit
Enter your choice 2
Deleted55
1.push 2.pop 3.display 4.exit
Enter your choice2
Deleted33
1.push 2.pop 3.display 4.exit
Enter your choice2
Deleted22
1.push 2.pop 3.display 4.exit
Enter your choice2
Deleted12
1.push 2.pop 3.display 4.exit
Enter your choice 2
Stack Underflow
1.push 2.pop 3.display 4.exit
Enter your choice 4
