1. 程式人生 > >C語言實現順序棧的括號匹配

C語言實現順序棧的括號匹配

//順序棧的使用舉例:括號的匹配
#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 256


typedef int Status; //函式返回狀態
typedef char SElemType;  //棧元素型別
typedef struct{//棧結構定義
    SElemType *base;
    SElemType *top;
    int stacksize;
}SqStack;




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);
                //printf("Push %c",*p);
                break;
            case ')':
                if(FALSE==GetTop(*S,&e))
                    return FALSE;
                if(e=='('){
                    if(ERROR==Pop(S,&e))
                        return FALSE;
                    //printf("Push %c",*p);
                }else
                    return FALSE;
                break;
            case ']':
                if(FALSE==GetTop(*S,&e))
                    return FALSE;
                if(e=='['){
                    if(ERROR==Pop(S,&e))
                        return FALSE;
                    //printf("Push %c",*p);
                }else
                    return FALSE;
                break;
            case '}':
                if(FALSE==GetTop(*S,&e))
                    return FALSE;
                if(e=='{'){
                    if(ERROR==Pop(S,&e))
                        return FALSE;
                    //printf("Push %c",*p);
                }else
                    return FALSE;
                break;
            default:
                ;
        }//switch
        p++;
    }//while
    if(!StackEmpty(*S))//字串遍歷完,棧非空,不匹配
        return FALSE;
    return TRUE;
}


int main()
{
    char *string;
    SqStack stack;
    string=(char*)malloc(sizeof(char)*BUFFERSIZE);
    if(!string){
        printf("分配記憶體失敗.\n");
        exit(0);
    }
    while(1){
        printf("請輸入一行含括號的表示式(輸入\"!\"退出):");
        gets(string);
        if(string[0]=='!')//退出
            break;
        if(TRUE==BracketMatch(&stack,string)){
            printf("\n能  正確匹配.\n\n");
        }else{
            printf("\n不能 正確匹配.\n\n");
        }
    }//while
    return 0;

}

執行結果: