forked from Anooppandikashala/youtube_c_tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_implementation.c
More file actions
125 lines (112 loc) · 2.02 KB
/
stack_implementation.c
File metadata and controls
125 lines (112 loc) · 2.02 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
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
#include <stdio.h>
#define MAX_ARRAY_SIZE 10
int stack_array[MAX_ARRAY_SIZE];
int top = -1;
int isEmpty()
{
//TODO code here
if (top == -1)
return 1;
return 0;
}
void print()
{
//TODO code here
if (isEmpty())
{
printf("Stack is empty");
return;
}
int i;
for (i = 0; i <= top; i++)
{
printf("\t%d", stack_array[i]);
}
}
int isFull()
{
//TODO code here
if (top == MAX_ARRAY_SIZE - 1)
return 1;
return 0;
}
void push()
{
//TODO code here
if (isFull())
{
printf("\nStack Overflow");
return;
}
int element;
printf("\nEnter element to add :");
scanf("%d", &element);
top = top + 1;
stack_array[top] = element;
print();
}
int pop()
{
//TODO code here
if (isEmpty())
{
printf("\nStack underflow");
return -1;
}
int element = stack_array[top];
top = top - 1;
print();
return element;
}
int getUserActions()
{
printf("\n1. Push");
printf("\n2. Pop");
printf("\n3. is Empty");
printf("\n4. is Full");
printf("\n5. print");
printf("\n6. Exit");
printf("\nEnter your choice : ");
int input;
scanf("%d", &input);
return input;
}
int main()
{
//TODO code here
int key = 1;
while (key)
{
int input = getUserActions();
switch (input)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
if (isEmpty())
printf("\nStack is empty");
else
printf("\nStack is not empty");
break;
case 4:
if (isFull())
printf("\nStack is full");
else
printf("\nStack is not full");
break;
case 5:
print();
break;
case 6:
key = 0;
break;
default:
printf("\nInvalid choice");
break;
}
}
}