-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
219 lines (180 loc) · 5.26 KB
/
Copy pathstack.cpp
File metadata and controls
219 lines (180 loc) · 5.26 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef union {
int *arrayOfInt;
char *arrayOfChar;
} Type;
struct Stack {
int top;
unsigned int capacity;
int currentAmount;
int stackCeiling;
int type;
Type array;
};
struct Stack *createStack(int stackCeiling, int type) {
struct Stack *stack = (struct Stack *)malloc(sizeof(struct Stack));
if (!stack) {
return NULL;
}
stack->capacity = 1;
stack->top = -1;
stack->stackCeiling = stackCeiling;
stack->type = type;
stack->currentAmount = 0;
if (!type) {
stack->array.arrayOfChar =
(char *)malloc(stack->capacity * sizeof(char));
if (!stack->array.arrayOfChar) {
free(stack);
return NULL;
}
} else {
stack->array.arrayOfInt = (int *)malloc(stack->capacity * sizeof(int));
if (!stack->array.arrayOfInt) {
free(stack);
return NULL;
}
}
return stack;
}
int extendStack(struct Stack *stack, unsigned int cap) {
if (cap > stack->stackCeiling)
return -1;
void *newArray;
if (!stack->type) {
newArray = realloc(stack->array.arrayOfChar, cap * sizeof(char));
} else {
newArray = realloc(stack->array.arrayOfInt, cap * sizeof(int));
}
if (!newArray) {
return -1;
}
if (!stack->type) {
stack->array.arrayOfChar = (char *)newArray;
} else {
stack->array.arrayOfInt = (int *)newArray;
}
stack->capacity = cap;
return 0;
}
void freeStack(struct Stack *stack) {
if (stack) {
if (!stack->type)
free(stack->array.arrayOfChar);
else
free(stack->array.arrayOfInt);
free(stack);
}
}
int isFull(struct Stack *stack) {
return stack->top == (int)stack->capacity - 1;
}
int isEmpty(struct Stack *stack) { return stack->top == -1; }
void push(struct Stack *stack, int value) {
if (isFull(stack)) {
if (stack->capacity >= stack->stackCeiling) {
printf("Stack ceiling reached at %d, cannot push %d\n",
stack->stackCeiling, value);
return;
}
stack->currentAmount++;
unsigned int newCap = stack->capacity * 2;
if (newCap > stack->stackCeiling)
newCap = stack->stackCeiling;
if (extendStack(stack, newCap)) {
printf("Failed to extend stack, cannot push\n");
return;
}
printf("Stack extended to capacity: %d\n", stack->capacity);
}
if (!stack->type) {
stack->array.arrayOfChar[++stack->top] = (char)value;
printf("Pushed %c to stack\n", (char)value);
} else {
stack->array.arrayOfInt[++stack->top] = value;
printf("Pushed %d to stack\n", value);
}
return;
}
int pop(struct Stack *stack) {
if (isEmpty(stack)) {
printf("Stack underflow\n");
return INT_MIN;
}
int value;
if (!stack->type) {
value = (int)stack->array.arrayOfChar[stack->top];
printf("Popping: %c from the stack\n", value);
} else {
value = stack->array.arrayOfInt[stack->top];
printf("Popping: %d from the stack\n", value);
}
stack->currentAmount--;
stack->top--;
return value;
}
int peek(struct Stack *stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
return INT_MIN;
}
if (!stack->type)
return (int)stack->array.arrayOfChar[stack->top];
else
return stack->array.arrayOfInt[stack->top];
}
int checkParen(struct Stack *st, char *string) {
int size = strlen(string);
if (!size)
return -1;
int count = 0;
for (int i = 0; i < size; i++) {
if (string[i] == '(' || string[i] == '{' || string[i] == '[') {
if (count > (size - count)) {
int position = size - count - st->currentAmount - 1;
printf("Error at position %d\n", position + 1);
printf("%s\n", string);
for (int j = 0; j < position; j++)
printf(" ");
printf("^ Extra opening bracket starts here\n");
return 0;
}
push(st, string[i]);
count++;
printf("count %d\n", count);
} else {
if (isEmpty(st)) {
printf("Error at position %d\n", i);
printf("%s\n", string);
for (int j = 0; j < i; j++)
printf(" ");
printf("^ Extra closing bracket starts here\n");
return 0;
}
int top = pop(st);
if ((top == '(' && string[i] != ')') ||
(top == '{' && string[i] != '}') ||
(top == '[' && string[i] != ']')) {
printf("%c and %c do not match at position %d in %s\n", top,
string[i], i, string);
return 0;
}
}
}
return 1;
}
int main() {
char *string = "{[{[[(())]]}]})";
int len = strlen(string);
struct Stack *st = createStack(20, 0);
if (!st) {
printf("Failed to create stack.\n");
return 1;
}
(checkParen(st, string) == 1) ? printf("Parens match\n") : -1;
freeStack(st);
return 0;
}