-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStackImplementation.c
73 lines (68 loc) · 1.25 KB
/
StackImplementation.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
#include <stdio.h>
#define MAX_SIZE 20
int topOfStack = -1;
int stack[MAX_SIZE];
int main()
{
while (1)
{
int keyPress;
printf("\n\nPress a key according to the operation you want.\n1. Push\n2. Pop\n3. Display\n: ");
scanf("%d", &keyPress);
switch (keyPress)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
default:
printf("Enter a vaild option!");
break;
}
}
}
void push()
{
if (topOfStack == 20)
{
printf("\nStack Overflow!\nCan't add any more items.");
}
else
{
printf("\nEnter the element to push into the stack: ");
scanf("%d", &stack[++topOfStack]);
printf("\nPushed successfully!");
}
}
void pop()
{
if (topOfStack == -1)
{
printf("\nStack Underflow!");
}
else
{
printf("%d", stack[topOfStack--]);
}
}
void display()
{
int i;
if (topOfStack == -1)
{
printf("\nStack Underflow!");
}
else
{
printf("\nStack:\n");
for ( i = topOfStack; i > -1; i--)
{
printf("%d\n", stack[i]);
}
}
}