Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 3: Implementing a Stack using Arrays! Mainly the following basic functio

ID: 3914169 • Letter: P

Question

Problem 3: Implementing a Stack using Arrays! Mainly the following basic functions are performed in the stack: Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition. Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition. Display: Returns all elements of the stack in order. isEmpty: Returns true if the stack is empty, else false. Exit: To exit the program Example: rogram for Stack Operations 1.Push 2.Pop .Display .Exit mter your choice 1 mter a value 10 1.Push Pop .Display .Exit nter your choice 3 10 The stack elements are: 1. Push .Pop .Display .Exit mter your choice 4

Explanation / Answer

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class Stack
{
int stack[5];
int top;
public:
Stack()
{
top=-1;
}
//METHOD TO PUSH ELEMENT TO STACK
void push(int x)
{
if(top > 4)
{
cout <<"Stack over flow";
return;
}
stack[++top]=x;

}
//method to pop element from stack
void pop()
{
if(top <0)
{
cout <<"Stack under flow";
return;
}
stack[top--];
}
//method to display stack elements
void display()
{
if(top<0)
{
cout <<" Stack empty";
return;
}
cout<<"Stack elements : ";
for(int i=top;i>=0;i--)
cout <<stack[i] <<" ";
cout<<" ";
}
};
//tester method
main()
{
int ch;
Stack st;
cout<<"Stack Operations: ";
while(1)
{
cout <<" 1.Push 2.Pop 3.Display 4.Exit Enter 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);
}
}
return (0);
}

output:

Stack Operations:

1.Push
2.Pop
3.Display
4.Exit
Enter your choice :1
Enter the element :10

1.Push
2.Pop
3.Display
4.Exit
Enter your choice :3
Stack elements : 10

1.Push
2.Pop
3.Display
4.Exit
Enter your choice :1
Enter the element :20

1.Push
2.Pop
3.Display
4.Exit
Enter your choice :3
Stack elements : 20 10

1.Push
2.Pop
3.Display
4.Exit
Enter your choice :2

1.Push
2.Pop
3.Display
4.Exit
Enter your choice :3
Stack elements : 10

1.Push
2.Pop
3.Display
4.Exit
Enter your choice :2

1.Push
2.Pop
3.Display
4.Exit
Enter your choice :3
Stack empty
1.Push
2.Pop
3.Display
4.Exit
Enter your choice :4


Process exited normally.
Press any key to continue . . .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote