forked from aa25desh/CSLR21-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
126 lines (120 loc) · 2.46 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <stdio.h>
#define MAXSIZE 10
int stack[MAXSIZE], top = -1;
int isEmpty();
int isFull();
void Push(int element);
int Pop();
int Top();
void Display();
void main(void)
{
int choice, element, temp, flag = 1;
while(flag)
{
printf("\n\nStack Operations");
printf("\n----------------");
printf("\n1. Push");
printf("\n2. Pop");
printf("\n3. Top");
printf("\n4. Display");
printf("\n5. Exit");
printf("\n----------------");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
{
if(!isFull())
{
printf("\nEnter element to be pushed: ");
scanf("%d", &element);
Push(element);
printf("\nPushed Element: %d", element);
}
else
{
printf("\nStack Overflow..");
}
break;
}
case 2:
{
if(!isEmpty())
{
temp = Pop();
printf("Popped Element: %d ", temp);
}
else
{
printf("\nStack Underflow..");
}
break;
}
case 3:
{
if(!isEmpty())
{
temp = Top();
printf("\nTop Element: %d", temp);
}
else
{
printf("\nStack is Empty");
}
break;
}
case 4:
{
Display();
break;
}
case 5:
{
flag = 0;
break;
}
default:
{
printf("\nInvalid Choice");
}
}
}
}
int isEmpty()
{
if(top == -1) return 1;
return 0;
}
int isFull()
{
if(top == (MAXSIZE-1)) return 1;
return 0;
}
void Push(int element)
{
stack[++top] = element;
}
int Pop()
{
return stack[top--];
}
int Top()
{
return stack[top];
}
void Display()
{
int i;
printf("\nStatus of the stack: ");
if(isEmpty())
{
printf("\nStack is Empty");
return;
}
for(i = top; i >= 0; i--)
{
printf("\n%d", stack[i]);
}
}