2021-07-17

王道数据结构思维导图8:栈

在这里插入图片描述

顺序栈基本操作

typedef struct{
//初始化栈
      ElemType data[MaxSize];
      int top;
    }SqStact;
 //进栈
 //s.top=-1
 bool Push(SqStact &s,ElemType &x){
      if(s.top==Maxsize-1)
         return false;
      //先加1,或赋值	
      s.top++;	
      s.data[s.top]=x;
      return true;
 }   
  //s.top=0
   bool Push(SqStact &s,ElemType &x){
      if(s.top==Maxsize-1)
         return false;	
      //先赋值,在加1
      s.data[s.top]=x;
      s.top++;
      return true;
 }   
 //出栈
 //s.top=-1;
 bool Pop((SqStact &s,ElemType &x) {
       if(s.top==-1)
          return false;
        x=s.data[s.top];
        s.top=s.top-1;
        return true;
    }
 //s.top=0; 
 bool Pop((SqStact &s,ElemType &x) {
       if(s.top==-1)
          return false;
        s.top=s.top-1;
        x=s.data[s.top];
        return true;
    }

栈在括号匹配中应用

bool barcketCheck(char str(),int length){
     //初始化一个栈
     SqStack S;
     InitStack(S)
     for(int i=0;i<length;i++){
     if(str[i]=='('||'['||'{'){
          Pop(S,str[i]);
          }else{
          //判断栈是否为空
             if(StackEmpty(S))
                  return false;
             //topElem为栈顶元素
             char topElem;
             Pop(S,topElem);
             if(ste[i]=')'&&topElem!='(')
                 return false;
             if(ste[i]=']'&&topElem!='[')
                return false; 
             if(ste[i]='}'&&topElem!='{')
                return false; 
             }  
     }
     return stackEmpty(S);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值