-
Notifications
You must be signed in to change notification settings - Fork 0
/
stackArray.c
82 lines (81 loc) · 1.31 KB
/
stackArray.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
#include <stdio.h>
#include <stdlib.h>
// stack is a data structure that follows LIFO principle
int stack[100], max = 99, top = -1;
int push(int data);
void pop();
void peek();
void display();
int main()
{
int option, data;
printf("Choose from menu\n");
printf("1.push\n");
printf("2.pop\n");
printf("3.peek\n");
printf("4.display\n");
while (1)
{
printf("Choose option\n");
scanf("%d", &option);
switch (option)
{
case 1:
printf("Enter data\n");
scanf("%d", &data);
push(data);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
default:
printf("Exit\n");
exit(1);
}
}
}
int push(int data)
{
if (top == max)
{
printf("Stack Overflow\n");
}
else
{
top++;
stack[top] = data;
}
return top;
}
void pop()
{
if (top == -1)
{
printf("Stack Underflow\n");
}
else
{
top--;
}
}
void peek()
{
printf("%d", stack[top]);
printf("\n");
}
void display()
{
int i = 0;
while (i <= top)
{
printf("%d ", stack[i]);
i++;
}
printf("\n");
}