括号配对

#include <stdio.h>
#include <stdlib.h>

#define OK      1
#define ERROR   0
#define TRUE    1
#define FALSE   0
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define BUFFERSIZE 1010

typedef int Status; //函数返回状态
typedef char SElemType;  //栈元素类型
typedef struct{//栈结构定义
    SElemType *base;
    SElemType *top;
    int stacksize;
}SqStack;

Status InitStack(SqStack *S);
    //构造一个空栈S
Status DestroyStack(SqStack *S);
    //销毁栈S,S不再存在
Status ClearStack(SqStack *S);
    //把栈S置为空栈
Status StackEmpty(SqStack S);
    //若栈S为空栈,则返回TRUE,否则返回FALSE
int StackLength(SqStack S);
    //返回S元素的个数,即栈的长度
Status GetTop(SqStack S,SElemType *e);
    //若栈不为空,则用e返回S的栈顶元素,并返回OK;否则返回FALSE
Status Push(SqStack *S,SElemType e);
    //插入元素e为新的栈顶元素
Status Pop(SqStack *S,SElemType *e);
    //若栈S不为空,则删除S的栈顶元素,用e返回其值,并返回OK,否则返回ERROR
Status StackTraverse(const SqStack *S);
    //从栈底到栈顶依次对每个元素进行访问
Status BracketMatch(SqStack *S,const char *string);
    //输入字符串string中的括号匹配,返回TRUE;否则返回FALSE

int main()
{
    char *string;
int   N;
    SqStack stack;
scanf("%d", &N);
    while(N--) {
string=(char*)malloc(sizeof(char)*BUFFERSIZE);
if(!string){
        printf("分配内存失败.\n");
        exit(0);
}
scanf("%s",string);
        if(TRUE==BracketMatch(&stack,string)){
            printf("Yes\n");
        }else{
            printf("No\n");
        }
    }//while
    return 0;
}

Status InitStack(SqStack *S){
    //构造一个空栈S
    S->base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
    if(!S->base)//分配失败
    {
        printf("分配内存失败.\n");
        exit(0);
    }
    S->top=S->base;
    S->stacksize=STACK_INIT_SIZE;
    return OK;
}

Status DestroyStack(SqStack *S){
    //销毁栈S,S不再存在
    if(!S)//S为空
    {
        printf("指针为空,释放失败.\n");
        exit(0);
    }
    free(S);
    return OK;
}

Status ClearStack(SqStack *S){
    //把栈S置为空栈
    if(!S)//S不存在
        return FALSE;
    S->top=S->base;//直接将栈顶指针指向栈底
    return OK;
}

Status StackEmpty(SqStack S){
    //若栈S为空栈,则返回TRUE,否则返回FALSE
    if(S.top==S.base)
        return TRUE;
    else
        return FALSE;
}

int StackLength(SqStack S){
    //返回S元素的个数,即栈的长度
    return S.stacksize;
}

Status GetTop(SqStack S,SElemType *e){
    //若栈不为空,则用e返回S的栈顶元素,并返回OK;否则返回FALSE
    if(S.top==S.base){
        //printf("栈为空.\n");
        return FALSE;
    }else{
        *e=*(S.top-1);
        //printf("%c",*e);
        return OK;
    }
}

Status Push(SqStack *S,SElemType e){
    //插入元素e为新的栈顶元素
    if(S->top-S->base>=S->stacksize){//栈已满,追加存储空间
        S->base=(SElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType));
        if(!S->base)
        {
            printf("重新申请空间失败.\n");
            exit(0);
        }
        S->top=S->base+S->stacksize;//更改栈顶指针
        S->stacksize+=STACKINCREMENT;
    }
    *S->top++=e;
    return OK;
}

Status Pop(SqStack *S,SElemType *e){
    //若栈S不为空,则删除S的栈顶元素,用e返回其值,并返回OK,否则返回ERROR
    if(S->top==S->base){//栈为空
        printf("栈为空.\n");
        return ERROR;
    }
    *e=*(--S->top);
    return OK;
}

Status StackTraverse(const SqStack *S){
    //从栈底到栈顶依次对每个元素进行访问
    SElemType *p=S->base;
    if(S->base==S->top)
    {
        printf("栈为空.\n");
        return FALSE;
    }
    //printf("栈中元素:");
    while(p!=S->top)
    {
        printf("%c",*p++);
    }
    printf("\n");
    return OK;
}

Status BracketMatch(SqStack *S,const char *string){
    //输入字符串string中的括号匹配,返回TRUE;否则返回FALSE
    const char *p=string;
    SElemType e;
    InitStack(S);
    while(*p!='\0'){//遍历字符串
        switch(*p){//判断p的值
            case '('://左括号,入栈
            case '[':
            case '{':
                Push(S,*p);
                break;
            case ')':
                if(FALSE==GetTop(*S,&e))
                    return FALSE;
                if(e=='('){
                    if(ERROR==Pop(S,&e))
                        return FALSE;
                }else
                    return FALSE;
                break;
            case ']':
                if(FALSE==GetTop(*S,&e))
                    return FALSE;
                if(e=='['){
                    if(ERROR==Pop(S,&e))
                        return FALSE;
                }else
                    return FALSE;
                break;
            case '}':
                if(FALSE==GetTop(*S,&e))
                    return FALSE;
                if(e=='{'){
                    if(ERROR==Pop(S,&e))
                        return FALSE;
                }else
                    return FALSE;
                break;
            default:
                ;
        }//switch
        p++;
    }//while
    if(!StackEmpty(*S))//字符串遍历完,栈非空,不匹配
        return FALSE;
    return TRUE;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值