PROGRAM
mplement push, pop, and display operations on stick of
priate mess
gers. The program
should pr
es for stack overflow, stack U
jerflow & stack em
ty
Deseripti
Stack:It is an ordered collection of data elements into which new elements may be inserted and from
which elements may be deleted at one end called the "FOP" of stack.
A stack is a lastein-first-out (LIPO ) structure
Insertion operation is referred as “PUSH” and deletion ope
Lion is referred as “POP
The most accessible element in the st
wack is the clement at the position “TOP?
SI
tack must be ereated as empty
Whenever an element is pushed into stack, it must be checked whether the stack isfull or not
Whenever an clement is popped form stack, it must be
checked whether the stack isempty or not
We ean implement the stack ADT either with array or linked list
Us) Top
SS
Stack of coins Stack of books
Computer stack
ALGORITHM: push()
Step 1: if top> =max-1 then
Step 2: Display the stack overflowsStep 3: else then
Step 4: top ++
Step 5: assign stack{top}=x
Step 6: Display eter
ent is insertedALGORITHM pop()
Step 1: iftop ==-1 then
Step 2: Display the stack is underflowsStep 3: else
Step 4: assign x=stack{top|Step 5; top- -
Step 6: return xSOURCE CODE:
include iostream,
using namespace std;
‘class Stack
{int alSI:
int index;
public:
{index = -15
void displays
void push(int):
i int pop):
bool isEmpty;
‘ull:
bool is
bool Stack:isEmpty()
{ return (index =
1) ime : fa
bool Stack::isFull()
{ return (index
5)? true : fal
int Stack::pop()
{
if (isEmpty)
cout << "\"STACK IS EMPTY !\n";
return 0;
index
return afindex + 1};
void Stack::push(int x)
4
indext+;
if (isFull())
{ cout <<"\nSTACK IS ALREADY FULLI\n";
index--;
~ —_—ereturn:
afindex] =
void Stack::display()
irtisEmpty0)
{
‘cout << "WMSTACK IS EMPTY \n";
returns
cout << "WNELEMENTS IN THE STACK :":
for (int i= 0: 1 <= index; i++)
cout << afi] <
void menu()
cout << "\MPRESS 1, for PUSHING element";
cout << "\nPRESS 2, for POPPING OUT element";
cout << "\nPRESS 3, for DISPLAYING elements in the stack";
cout << "\nPRESS 4, for checking isEmpty st
cout <<"\nPRESS 5, for checking isFull stack";
cout <<"\nPRESS 6, for EXIT"; return;
}
int main()
int choice, x; Stack stk;
while (true)
menu();
cout << "\nENTER YOUR CHOICE : "
cin >> choice; switch (choice)
tease 1:
cout << "\nENTER ELEMENT
cin >> x;
stk.push(x);ie
break:
case 2:
x= stk.pop);
cout << "\npopped element is" << x,
break;
case 3:
stk.display);
break;
case 4:
if (stk.isEmpty())
cout << "\NYES.STACK IS EMPTY \\n";
else
cout <<"\NO.STACK IS NOT EMPTY !\n";
break;
case 5: if (stk.isFull())
cout << "\NYES.STACK IS FULL";
else cout << "\WNO.STACK IS NOT FULL!";
break;
case 6: cout << "\nEXITING!!\n";
exit(O);
default: cout << "\nINVALID INPUT\n";
\
1
return 0;ouTPUT
Sosy
ree
ey
Pte ts
ee tri
ete ee iia!
3 5, for checking isPull stack
ect
4 a
ed
eet ee
ea ae ener ane
Seo re cree
Seo ree
og)
eee
eed
et
Pa Cee yen
ES3 4, for checking istmpty stack
Peeper ay
Sar
3
Se eee
ed
eed
ert
ere rs
caer eet
eos)