-
Notifications
You must be signed in to change notification settings - Fork 13
/
brackets-extreme-edition.c
37 lines (31 loc) · 1.01 KB
/
brackets-extreme-edition.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
#include <stdio.h>
struct l_list {
char data;
struct l_list *next;
};
void push(struct l_list** ref, int new_data) {
struct l_list* l_new = (struct l_list*) malloc(sizeof(struct l_list));
l_new->data = new_data;
l_new->next = *ref;
*ref = l_new;
}
char pop(struct l_list** ref) {
struct l_list *top = *ref;
char result = top->data;
*ref = top->next;
free(top);
return result;
}
int matching(const char c1, const char c2) {
return ( (c1 == '{' && c2 == '}') || (c1 == '[' && c2 == ']') || (c1 == '(' && c2 == ')') );
}
void main() {
struct sNode *stack = NULL;
char e[2049];
scanf("%s", e);
for (int i = 0; i < strlen(e); ++i)
if (e[i] == '{' || e[i] == '(' || e[i] == '[') push(&stack, e[i]);
else if (e[i] == '}' || e[i] == ')' || e[i] == ']')
if ( ( stack == NULL ) || ( !matching(pop(&stack), e[i]) ) ) { printf("false\n"); return; }
printf("%s", (stack == NULL) ? "true\n" : "false\n");
}