堆栈数据结构应用之括号匹配检查(算法3.2.2)

#include <stdio.h>

#include <malloc.h>

#include <string.h>

#define STACK_INIT_SIZE         100

#define STACK_INCRASE_SIZE  20

 

enum State{ERR,OK,FAILED};

typedef char SElemType;

typedef struct tag_Stack              //此空递增

{

       SElemType*   pbase;                   //栈底指针

       SElemType*   ptop;                     //栈顶指针

       int                  stacksize;              //当前所有可用栈空间(包含已使用的)

}SqStack;

 

void InitStack(SqStack* ps)

{

       ps->pbase = (SElemType*)calloc(STACK_INIT_SIZE,sizeof(SElemType));     //分配失败处理略

 

       ps->ptop = ps->pbase;

       ps->stacksize = STACK_INIT_SIZE;

}

 

void push(SqStack* ps,SElemType* pdata)

{

       if(ps->ptop - ps->pbase >= ps->stacksize)   //判断是否栈溢出(允许范围)

       {

              ps->pbase = (SElemType*)realloc(ps->pbase,(ps->stacksize+STACK_INCRASE_SIZE)*sizeof(SElemType));

              //分配失败处理略

              ps->ptop = ps->stacksize + ps->pbase;

              ps->stacksize += STACK_INCRASE_SIZE;

       }

       *ps->ptop++ = *pdata;         //有覆盖吧?  这是写入一个字串阿,不是字串   细细品味一哈

}

 

enum State pop(SqStack* ps,SElemType* pdata) //出栈pdata

{

       if(ps->ptop == ps->pbase) return ERR;

       *pdata = *(ps->ptop-1);

       *(ps->ptop-1) = 0;        //为了监视直观写此语句,正式运行时不需要,浪费效率

       ps->ptop--;

       return OK;

}

 

enum State GetAtTop(SqStack* ps,SElemType* pdata)

{

       if(ps->ptop == ps->pbase) return ERR;

       *pdata = *(ps->ptop-1);

       return OK;

}

 

int IsStackEmpty(SqStack* ps)

{

       if(ps->ptop == ps->pbase) return 1;

       return 0;

}

 

int IsPipei(char ch1,char ch2)

{

       switch(ch1)

       {

       case '[':

              if(']'==ch2) return 1;

              break;

       case '(':

              if(')'==ch2) return 1;

              break;

       case '{':

              if('}'==ch2) return 1;

              break;

       default:

              break;

       }

       return 0;

}

 

enum State CheckKuohu(SqStack* ps,char* pd)

{

       int l = 0, i = 0;

       char* ptmp = NULL,buf = 0;

 

       ptmp = pd;    

       push(ps,(SElemType*)pd++);      

      

       do

       {

              SElemType tmp2 = 0;

              GetAtTop(ps,&tmp2);                  //栈顶元素保存到tmp2

              if(IsPipei(tmp2,(char)*pd))   //有点不规范,发生截断

              {

                     pop(ps,&buf);               //若匹配,则弹出

              }

              else

              {

                     push(ps,(SElemType*)pd);

              }

              pd++;

       }while(*pd);

       if(IsStackEmpty(ps)) return OK;

      

       return FAILED;

}

 

void main()

{

       char* pdata = "{[([]{})[]([])]}";

       SqStack Myps;                    //定义栈管理对象

       InitStack(&Myps);

       if(OK == CheckKuohu(&Myps,pdata))

       {

              printf("匹配");

       }

       else

       {

              printf("不匹配阿!");

       }

 

       return;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值