Write a program that implements the stack ADT described in
. Write a program that implements the stack ADT described in Section 3.4, “Stack ADT.” To test your implementation, write a menu-driven user interface to test each of the operations in the ADT. For the test, use integer data as described below. Error conditions should be printed as a part of the test results. A suggested menu is shown below. Test your program with the following test case. You may include additional test cases. The menu operation is shown at the end of the test. a. Print stack status: Empty [E] b. Pop and print data (should return error) [B] c. Push data into stack: 1 [A] d. Push data into stack: 2 [A] e. Print stack status: Empty [E] f. Print stack status: Full [F] g. Print data at top of stack [C] h. Print entire stack (top to base) [D] i. Print number of elements in stack [G] j. Pop and print data [B] k. Pop and print data [B] l. Pop and print data (should return empty) [B] m. Push data into stack: 3 [A] n. Print data at top of stack [C] o. Destroy stack and quit [H]
Solution
#include<iostream>
 #include<conio.h>
 #include<stdlib.h>
 using namespace std;
 class stack
 {
 int stk[2]; //Stack size
 int top; //Top position
 public:
 stack() //Constructor
 {
 top = -1; //Initialize top to -1
 }
 //Push operation
 void push()
 {
 int x;
 //Checks for Stack full
 if(top >= 1)
 {
 cout <<\"\  Stack Status: Full\";
 return;
 }
 //Accepts data
 cout <<\"\  Enter the element: \";
 cin >> x;
 //Increase the stack top and stores data
 stk[++top] = x;
 cout <<\"\  Push data into stack: \"<<x;
 }
 //Stack top value displayed
 void TopPos()
 {
 cout<<\"\  Top Element: \"<<stk[top];
 return;
 }
 //Pop element from stack
 void pop()
 {
 //Checks for Empty
 if(top < 0)
 {
 cout <<\"\  Error: Stack Underflow \";
 return;
 }
 //Displays the deleted item
 cout <<\"\  Deleted\" <<stk[top--];
 }
 //Displays the complete stack data
 void display()
 {
 //Checks stack is empty or not
 if(top < 0)
 {
 cout <<\"\  Stack status: Empty \";
 return;
 }
 for(int i = top;i >= 0; i--)
 cout <<stk[i] <<\" \";
 }
//Count and display number of elements in the stack
 void countEle()
 {
 int co = 0;
 for(int i = top; i >= 0; i--)
 co++;
 cout<<\"\  Number of element in the stack: \"<<co;
 }
 };
main()
 {
 char ch;
 stack st; //Creates an object of stack
 //Loops till H is pressed
 while(1)
 {
cout<<\"\  B. POP \";
 cout<<\"\  A. PUSH \";
 cout<<\"\  C. TOP \";
 cout<<\"\  D. Display \";
 cout<<\"\  G. Count \";
 cout<<\"\  H. Exit \";
 cout<<\"\  Enter your choice: \";
 cin >> ch;
 switch(ch)
 {
case \'B\':
 st.pop(); break;
 case \'A\':
 st.push();
 break;
 case \'C\':
 st.TopPos(); break;
 case \'D\':
 st.display();break;
 case \'G\':
 st.countEle(); break;
 case \'H\':
 exit(0);
 }
 }
 return (0);
 }
Output:
 B. POP
 A. PUSH
 C. TOP
 D. Display
 G. Count
 H. Exit
 Enter your choice: D
Stack status: Empty
 B. POP
 A. PUSH
 C. TOP
 D. Display
 G. Count
 H. Exit
 Enter your choice: B
Error: Stack Underflow
 B. POP
 A. PUSH
 C. TOP
 D. Display
 G. Count
 H. Exit
 Enter your choice: A
Enter the element: 1
Push data into stack: 1
 B. POP
 A. PUSH
 C. TOP
 D. Display
 G. Count
 H. Exit
 Enter your choice: A
Enter the element: 2
Push data into stack: 2
 B. POP
 A. PUSH
 C. TOP
 D. Display
 G. Count
 H. Exit
 Enter your choice: A
Stack Status: Full
 B. POP
 A. PUSH
 C. TOP
 D. Display
 G. Count
 H. Exit
 Enter your choice: C
Top Element: 2
 B. POP
 A. PUSH
 C. TOP
 D. Display
 G. Count
 H. Exit
 Enter your choice: D
 2 1
 B. POP
 A. PUSH
 C. TOP
 D. Display
 G. Count
 H. Exit
 Enter your choice: G
Number of element in the stack: 2
 B. POP
 A. PUSH
 C. TOP
 D. Display
 G. Count
 H. Exit
 Enter your choice: B
Deleted2
 B. POP
 A. PUSH
 C. TOP
 D. Display
 G. Count
 H. Exit
 Enter your choice: B
Deleted1
 B. POP
 A. PUSH
 C. TOP
 D. Display
 G. Count
 H. Exit
 Enter your choice: B
Error: Stack Underflow
 B. POP
 A. PUSH
 C. TOP
 D. Display
 G. Count
 H. Exit
 Enter your choice: A
Enter the element: 3
Push data into stack: 3
 B. POP
 A. PUSH
 C. TOP
 D. Display
 G. Count
 H. Exit
 Enter your choice: C
Top Element: 3
 B. POP
 A. PUSH
 C. TOP
 D. Display
 G. Count
 H. Exit
 Enter your choice: H





