1
0
mirror of https://github.com/Didnelpsun/CS408.git synced 2026-02-09 13:45:48 +08:00

串更新

This commit is contained in:
Didnelpsun
2021-04-22 23:31:40 +08:00
parent ce1f4aec3e
commit 9528d486d6
6 changed files with 164 additions and 19 deletions

View File

@@ -64,4 +64,36 @@ int GetSequenceStackTop(SequenceStack* stack, element_type* elem) {
// 弹出后再出栈
*elem = stack->data[stack->top];
return 0;
}
// 顺序栈实现括号匹配
int BracketCheck(char str[], int length) {
SequenceStack s;
// 初始化栈
InitSequenceStack(&s);
for (int i = 0; i < length; i++) {
// 扫描到左括号
if (str[i] == '{' || str[i] == '[' || str[i] == '(') {
PushSequenceStack(&s, str[i]);
}
else {
// 如果栈空
if (IsSequenceStackEmpty(s)==1) {
return 1;
}
}
// 定义一个栈顶元素变量
char t;
PopSequenceStack(&s, &t);
if (str[i] == ')' && t != '(') {
return 1;
}
if (str[i] == ']' && t != '[') {
return 1;
}
if (str[i] == '}' && t != '{') {
return 1;
}
}
return 0;
}