-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
85 lines (81 loc) · 1.34 KB
/
stack.c
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<stdio.h>
int stack[20],choice,n,top=-1,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
printf("\n Enter the size of stack[max=20]:");
scanf("%d",&n);
do
{
printf("\n\t 1.push\n\t 2.pop\n\t 3.display\n\t 4.exit");
printf("\n enter your choise:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
break;
}
default:
{
printf("\n\t please enter a valid choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
void push(){
if(top==n-1)
{
printf("\n\t stack is overflow");
}
else
{
printf("enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop(){
if(top==-1)
{
printf("\n\t stack is underflow");
}
else{
printf("\n\t the popped element is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n the element in stack \n");
for(i=top;i>=0;i--)
printf("\n%d",stack[i]);
printf("\n press next choice");
}
else
{
printf("\n the stack is empty");
}
}