-
Notifications
You must be signed in to change notification settings - Fork 7
/
stack.c
106 lines (89 loc) · 1.4 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
104
105
106
#include <stdio.h>
#include <stdbool.h>
int top =-1, n;
bool loop = false;
// stack push
void push(int a[])
{
if (top == n-1)
{
printf("Stack Overflow");
}
else
{
top =top+1;
printf("Enter value into the stack \t");
scanf("%d",&a[top]);
}
}
void pop(int a[])
{
if (top == -1)
printf("Stack empty");
else
{
printf("Element removed is %d",a[top]);
top = top-1;
}
}
void peek(int a[])
{
if (top == -1)
printf("Stack underflow");
else
printf("Element at the top of the stack is %d",a[top]);
}
void isFull(int a[])
{
if (top == n-1)
printf("Stack is full");
else
printf("Stack is not full");
}
void isEmpty(int a[])
{
if (top == -1)
printf("Stack is empty");
else
printf("Stack is not empty");
}
void exit_stack()
{
loop = true;
}
void main()
{
printf("Enter the size of the stack .");
scanf("%d",&n);
int a[n];
int choice;
while (loop == false)
{
printf("\n Menu \n 1.Push \n 2.Pop \n 3.Peek \n 4.isFull \n 5.isEmpty \n 6. Exit \n Enter your choice (1-5) \t");
scanf("%d",&choice);
switch (choice)
{
case (1):
push(a);
break;
case (2):
pop(a);
break;
case (3):
peek(a);
break;
case(4):
isFull(a);
break;
case(5):
isEmpty(a);
break;
case (6):
exit_stack();
break;
default:
printf("Invalid choice");
break;
}
}
}