forked from vedant1771/Hactoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
103 lines (88 loc) · 1.7 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include<stdio.h>
#define MAX 5
struct Stack
{
int stack[MAX];
int sp;
}s;
void init()
{
s.sp=-1;
}
int isFull()
{
if (s.sp==MAX-1)
return 1;
else
return 0;
}
int isEmpty()
{
if (s.sp==-1)
return 1;
else
return 0;
}
void push(int value)
{
if (isFull())
printf("Overflow");
else
s.stack[++s.sp]=value;
}
int pop()
{
if (isEmpty())
printf("Underflow");
else
return (s.stack[s.sp--]);
}
void display()
{
int tos=s.sp;
printf("\nStack values \n");
for (int i=tos;i>-1;i--)
printf(" %d",s.stack[i]);
}
int main()
{
int choice;
init();
while(1)
{
printf("\n1. PUSH");
printf("\n2. POP");
printf("\n3. IS FULL?");
printf("\n4. IS EMPTY?");
printf("\n5. DISPLAY");
printf("\n6. EXIT");
printf("\n\n");
printf("ENTER YOUR CHOICE");
scanf("%d",&choice);
int value;
switch (choice)
{
case 1 :printf("\nEnter value to be pushed ");
scanf("%d",&value);
push(value);
break;
case 2 :value=pop();
printf("\nValue popped : %d",value);
break;
case 3 :if (isFull())
printf("\nyes");
else
printf("\nno");
break;
case 4 :if (isEmpty())
printf("\nyes");
else
printf("\nno");
break;
case 5 :display();
break;
case 6 : exit(0);
default:printf("\nWrong choice");
}
}
}