一道数据结构题目,哪个高手说说怎么解啊!

2024-11-29 20:49:14
推荐回答(1个)
回答1:

int matching(SqList L){

//判定顺序表L所保存算术表达式中括号是否匹配,若匹配则返回1,否则返回0

InitStack(S); //初始化辅助栈S

for (i=0;i

e=L.elem[i];

switch(e){

case ‘(':

case ‘[':

case ‘{': Push(S,e);break;

case ‘)': if (StackEmpty(S)) return ERROR; //右括号个数多于左括号

Pop(S,k);

if (k!=‘(') return ERROR; //括号嵌套错

break;

case ‘]': if (StackEmpty(S)) return ERROR;

Pop(S,k);

if (k!=‘[') return ERROR;

break;

case ‘}': if (StackEmpty(S)) return ERROR;

Pop (S,k);

if (k!=‘{') return ERROR;

}//switch

}//for

if (EmptyStack(S)) return OK; //括号匹配

else return ERROR; //左括号个数多于右括号

}//matching