-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_func.h
More file actions
140 lines (118 loc) · 2.7 KB
/
stack_func.h
File metadata and controls
140 lines (118 loc) · 2.7 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#ifndef STACK_FUNC_H
#define STACK_FUNC_H
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
typedef struct Stack {
int items[MAX];
float fItems[MAX];
char cItems[MAX];
int topI;
int topF;
int topC;
} Stack;
Stack stack;
void initStack(int max) {
stack.topI = -1;
stack.topF = -1;
stack.topC = -1;
}
int isFullI() {
return stack.topI == MAX - 1;
}
int isFullF() {
return stack.topF == MAX - 1;
}
int isFullC() {
return stack.topC == MAX - 1;
}
int isEmptyI() {
return stack.topI == -1;
}
int isEmptyF() {
return stack.topF == -1;
}
int isEmptyC() {
return stack.topC == -1;
}
void pushI(int value) {
if (isFullI()) {
printf("Stack overflow: Cannot push %d, int stack is full\n", value);
return;
}
stack.items[++stack.topI] = value;
}
void pushF(float value) {
if (isFullF()) {
printf("Stack overflow: Cannot push %.2f, float stack is full\n", value);
return;
}
stack.fItems[++stack.topF] = value;
}
void pushC(char value) {
if (isFullC()) {
printf("Stack overflow: Cannot push '%c', char stack is full\n", value);
return;
}
stack.cItems[++stack.topC] = value;
}
int popI() {
if (isEmptyI()) {
printf("Stack underflow: Cannot pop, int stack is empty\n");
return -1;
}
return stack.items[stack.topI--];
}
float popF() {
if (isEmptyF()) {
printf("Stack underflow: Cannot pop, float stack is empty\n");
return -1.0f;
}
return stack.fItems[stack.topF--];
}
char popC() {
if (isEmptyC()) {
printf("Stack underflow: Cannot pop, char stack is empty\n");
return '\0';
}
return stack.cItems[stack.topC--];
}
void printStackI() {
if (isEmptyI()) {
printf("Int stack is empty\n");
return;
}
for (int i = 0; i <= stack.topI; i++) {
printf("%d ", stack.items[i]);
}
printf("\n");
}
void printStackF() {
if (isEmptyF()) {
printf("Float stack is empty\n");
return;
}
for (int i = 0; i <= stack.topF; i++) {
printf("%.2f ", stack.fItems[i]);
}
printf("\n");
}
void printStackC() {
if (isEmptyC()) {
printf("Char stack is empty\n");
return;
}
for (int i = 0; i <= stack.topC; i++) {
printf("'%c' ", stack.cItems[i]);
}
printf("\n");
}
void clearStack() {
stack.topI = -1;
stack.topF = -1;
stack.topC = -1;
printf("All stacks cleared\n");
}
#endif
//BY SOUHARDYA GHOSH
//MADE FOR MAKING CODING EASY IN C