int match(char exp[], int n) {
char stack[maxSize];
int top = -1;//define and initial a stack
int i;
for (i = 0; i < n; ++i) {
if (exp[i] == '(') {
stack[++top] = '(';//push stack
}
if (exp[i] == ')') {
if (top == -1) {
return 0;//if next bracket is ')' and the stack is empty
}
else
--top;
}
}
if (top == -1)
return 1;
else
return 0;
}
int main() {
char str[] = { 'a','c','(' ,')', '\0'};
int result = match(str, 5);
printf("%d", result);
}
使用顺序栈判断一个表达式中的括号是否正确配对
最新推荐文章于 2024-05-31 17:33:26 发布