数据结构,栈的应用,括号匹配算法

基本思想,将括号压入一个栈当中,若是左括号,则入栈,若是右括号,则出栈一个元素,看其是否匹配,若不匹配则匹配失败。
数据结构

typedef struct{
	char data[MaxSize];
	int top;
}SqStack;
void push(SqStack &s; char e)
{
    if(s.top==MaxSize) return 0;
    s.data[++s.top]=e;
}
char pop(SqStack &s; char &e)
{
    if(s.top==-1) return 0;
    e=s.data[s.top--];
    return e;
}
bool MatchBr(char *str)
{
   SqStack s;
   char c;
   int =0;
   while(str[i]!='\0')
   {
      switch(str[i])
      {
      	case '{':push(s; str[i]);
      	case '(':push(s; str[i]);
      	case '[':push(s; str[i]); //左括号入栈
      	case '}': c=pop(s; e);
      	if(c!={)  return 0;
      	break;
      	case ')': c=pop(s; e);
      	if(c!=()  return 0;
      	break;
      	case ']': c=pop(s; e);
      	if(c!=[)  return 0;
      	break;                    //右括号弹栈并比较
      	default: break;
      }
   }
   if(s.top==-1) //switch语句结束后栈空 说明所有匹配完成
   return 1;
   else return 0; //否则匹配失败
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值