-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
97 lines (94 loc) · 1.67 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
#include<stdio.h>
#include<stdlib.h>
void push();
void pop();
void peek();
void display();
int top=-1,arr[5];
void main()
{
printf("Maximum stack size is 5");
while(1)
{
int op;
printf("\nStack Operations");
printf("\n 1.Push");
printf("\n 2.Pop");
printf("\n 3.Peek");
printf("\n 4.Display");
printf("\n 5.Exit");
printf("\nSelect your operation : ");
scanf("%d",&op);
switch(op)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
exit(0);
default:
printf("\nSelect from 1,2,3,4,5 : \n\n");
}
}
}
void push()
{
int n;
if (top>=4)
{
printf("stack overflow");
}
else
{
printf("\nEnter element to be pushed : ");
scanf("%d",&n);
top=top+1;
arr[top]=n;
printf("%d added to stack.\n\n",n);
}
}
void pop()
{
if (top==-1)
{
printf("\nStack underflow.\n");
}
else
{
printf("\n%d popped.\n",arr[top]);
top=top-1;
}
}
void peek()
{
if (top==-1)
{
printf("\nNo element in stack\n");
}
else
{
printf("\nTop element is %d.\n",arr[top]);
}
}
void display()
{
if (top == -1)
{
printf("\nStack Underflow\n");
}
else
{
printf("\nElements in stack: \n");
for (int i = top; i >= 0; --i)
printf("%d\n",arr[i]);
}
}