Repository navigation
Expand file tree
/
Copy pathstackWarray.c
More file actions
96 lines (87 loc) · 1.44 KB
/
Copy pathstackWarray.c
File metadata and controls
96 lines (87 loc) · 1.44 KB
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>
#define N 50
int stack[N];
int top = -1;
void push()
{
int x;
printf("Enter The Element to push: ");
scanf("%d", &x);
if (top == N - 1)
{
printf("Stack overflow\n");
}
else
{
top++;
stack[top] = x;
printf("%d pushed to Stack\n", x);
}
}
void pop()
{
int item;
if (top == -1)
{
printf("Stack underflow\n");
}
else
{
int item = stack[top];
top--;
printf("Popped: %d\n", item);
}
}
void peek()
{
if (top == -1)
{
printf("Stack is empty\n");
}
else
{
printf("Top Element: %d\n", stack[top]);
}
}
void display()
{
int i;
printf("Displaying Stack....\n");
for (int i = top; i >= 0; i--)
{
printf("%d ", stack[i]);
}
printf("\n");
}
int main()
{
int ch;
while (1)
{
printf("\n*** Stack Menu ***");
printf("\n\n1.Insert\n2.Delete\n3.Display\n4.Peek\n5.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d", &ch);
switch (ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
peek();
break;
case 5:
exit(0);
default:
printf("\nWrong Choice!!");
}
}
return 0;
}