-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquoteMatch.c
123 lines (110 loc) · 2.15 KB
/
quoteMatch.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
#include <stdio.h>
#include <stdlib.h>
typedef struct lnode
{
char value;
struct lnode *next;
} lnode_t;
typedef struct cstack
{
int length;
lnode_t *head;
} cstack_t;
void lnode_init(lnode_t **tgt)
{
*tgt = (lnode_t *)malloc(sizeof(lnode_t));
(*tgt)->value = 0;
(*tgt)->next = NULL;
}
void cstack_init(cstack_t **tgt)
{
*tgt = (cstack_t *)malloc(sizeof(cstack_t));
(*tgt)->head = NULL;
(*tgt)->length = 0;
}
void cstack_push(cstack_t *tgt, char c)
{
lnode_t *node;
lnode_init(&node);
if (tgt->head == NULL)
{
node->next = NULL;
tgt->head = node;
}
else
{
node->next = tgt->head;
tgt->head = node;
}
tgt->length++;
tgt->head->value = c;
}
char cstack_pop(cstack_t *tgt)
{
if (tgt->head != NULL)
{
char c = tgt->head->value;
lnode_t *head = tgt->head;
tgt->head = tgt->head->next;
free(head);
return c;
}
else
{
return '0';
}
}
void cstack_destroy(cstack_t *tgt)
{
while (tgt->head != NULL)
{
lnode_t *head = tgt->head;
tgt->head = tgt->head->next;
free(head);
}
free(tgt);
}
int main(void)
{
char c;
cstack_t *quote_stack;
cstack_init("e_stack);
while ((c = getchar()) >= 32)
{
if (c == '(' || c == '[' || c == '{')
{
cstack_push(quote_stack, c);
}
else
{
switch (c)
{
case ')':
if (cstack_pop(quote_stack) != '(')
{
printf("no");
return 0;
}
break;
case ']':
if (cstack_pop(quote_stack) != '[')
{
printf("no");
return 0;
}
break;
case '}':
if (cstack_pop(quote_stack) != '{')
{
printf("no");
return 0;
}
break;
default:
break;
}
}
}
printf("yes");
return 0;
}