DSA Stack
DSA Stack
DATE
Assiqnment-3A
Afon
A menu dmves t program to inplement a
Stacr USig amray.
Theoyi
AStack a liear d t a Struhe wher
the elerments a e stored in an àrle ved
i r s t one to bc
mnoned
StauC Supports Few basic op erUhons
be w
C19 Push
an l ement
PUsh operanons USed to iasert
in to the Stac The ne elemet is adde d a
of Stak
to embSt pSihon the
Conduhon heccecl Shat
The only
hehher the stace iS
ofinserions Possible
Chee if TOp-0LL
the sra ck is
Represet aon
Afr caingpap
|i amroy seLLo, o, 3 J
30 30
3) Oispla
elemet
Tie Compie x S
Pop fun' ons ro
Compeny
Pusn opehon cI)
PAGE No.
DATE
ConcuSion
Forlouolng Oben ve we
USe
COncepts of
S21_100_Vedant Patil
Code:
#include<stdio.h>
#include<conio.h>
struct stack
{
int arr[10];
int top;
};
struct stack s1;
void display();
void pop();
void push();
void main()
{
int choice;
int n;
s1.top=-1;
do
{
printf("1.Push\n2.Pop\n3.Display\nEnter the choice to be performed: ");
scanf("%d",&choice);
switch(choice)
{
case 1://Perform Push
printf("Enter the number to be pushed: ");
scanf("%d",&n);
push(n);
break;
case 2://Perform Pop
pop();
break;
case 3://Display stack
display();
break;
default:
printf("Invalid choice");
break;
}
}while(choice<4);
}
void push(int n)
{
if(s1.top>=9)
{
printf("Stack is full\n");
S21_100_Vedant Patil
return;
}
s1.arr[++s1.top]=n;
}
void pop()
{
if(s1.top==-1)
{
printf("Stack at lowest level\n");
return;
}
printf("%d is the element popped\n",s1.arr[s1.top]);
s1.top--;
}
void display()
{
for(int i=s1.top;i>=0;i--)
{
printf("%d\n",s1.arr[i]);
}
if(s1.top==-1)
{
printf("Stack empty\n");
return;
}
}
Output:
1.Push
2.Pop
3.Display
Enter the choice to be performed: 1
Enter the number to be pushed: 12
1.Push
2.Pop
3.Display
Enter the choice to be performed: 1
Enter the number to be pushed: 13
1.Push
2.Pop
3.Display
Enter the choice to be performed: 1
Enter the number to be pushed: 14
S21_100_Vedant Patil
1.Push
2.Pop
3.Display
Enter the choice to be performed: 3
14
13
12
1.Push
2.Pop
3.Display
Enter the choice to be performed: 2
14 is the element popped
1.Push
2.Pop
3.Display
Enter the choice to be performed: 2
13 is the element popped
1.Push
2.Pop
3.Display
Enter the choice to be performed: 2
12 is the element popped
1.Push
2.Pop
3.Display
Enter the choice to be performed: 2
Stack at lowest level
1.Push
2.Pop
3.Display
Enter the choice to be performed: 3
Stack empty