-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBracktMatch.c
104 lines (100 loc) · 1.63 KB
/
BracktMatch.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
#include<stdio.h>
#include<stdlib.h>
typedef char Elemtype;
typedef struct LNode
{
Elemtype e;
struct LNode* next;
}LNode,*LinkStack;
void IniLStack(LinkStack* LS)
{
LNode * p = *LS;
p = (LNode*)malloc(sizeof(LNode));
if (p == NULL)
{
perror("IniLStack:");
printf("内存申请失败\n");
return;
}
p->next = NULL;
*LS = p;
return 1;
}
int isemptyStack(LinkStack LS)
{
if (LS->next == NULL)
{
printf("空栈\n");
return 1;
}
return 0;
}
int Puch(LinkStack LS,char c)
{
LNode* p = (LNode*)malloc(sizeof(LNode));
if (p==NULL)
{
perror("Puch:");
printf("内存申请失败\n");
return 0;
}
p->e = c;
p->next = LS->next;
LS->next = p;
return 1;
}
int Pop(LinkStack LS, char* c)
{
if (isemptyStack(LS))
{
printf("出栈失败\n");
return 0;
}
LNode* p = LS->next;
*c = p->e;
LS->next = p->next;
free(p);
p = NULL;
}
int BracktMatch(char arr[],int len)
{
LinkStack LS;
IniLStack(&LS);
for (int i = 0; i < len; i++)
{
if (arr[i] == '{' || arr[i] == '[' || arr[i] == '(')
{
Puch(LS, arr[i]);
}
else if (isemptyStack(LS))
{
printf("右括号单身");
return 0;
}
else
{
char topStack;
Pop(LS, &topStack);
if (arr[i] == '}' && topStack != '{')
return 0;
if (arr[i] == '[' && topStack != ']')
return 0;
if (arr[i] == ')' && topStack != '(')
{
printf("不匹配\n");
return 0;
}
}
}
if (isemptyStack==0)
{
printf("左括号单身\n");
return 1;
}
}
int main()
{
char arr[] = "()()()()()[){}";
int len = sizeof(arr) / sizeof(arr[0]) - 1;
BracktMatch(arr,len);
}