栈的应用之括号匹配问题

MatchBrackets.h

#ifndef __MatchBrackets_H__
#define __MatchBrackets_H__
#define OK 0;
#define ERROR 1;
#define STACK_INIT_SIZE 100
#define STACKINCREACE 10

typedef int ElemType;
typedef int Status;
typedef struct stack
{
    ElemType *base;
    ElemType *top;
    int stacksize;
}Stack;
Status InitStack(Stack* S);
Status PushStack(Stack* S , ElemType e);
Status PopStack(Stack* S);
Status GetStackTop(Stack S);
int StackLength(Stack S);
Status StackTraverse(Stack S);
Status ClearStack(Stack* S);
Status DestroyStack(Stack* S);
Status StackEmpty(Stack S);
Status MatchBrackets(Stack *S,char *str);


#endif  //__MatchBrackets_H__

MatchBrackets.c

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

int InitStack(Stack* S)
{
  S->base = (ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
   if(S->base == NULL)
   {
       printf("开辟栈空间失败\n");
       return ERROR;
   }
   S->stacksize  =  STACK_INIT_SIZE;
   S->top = S->base;
   return OK;
}
Status PushStack(Stack* S , ElemType e)
{
    if((S->top - S->base) == S->stacksize )//判断栈是否已满
    {
        S->base = (ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
        if(!S->base)
        {
          return ERROR;
        }
        S->top = S->base + S->stacksize;
        S->stacksize += STACKINCREACE;
    }
    //压栈部分
       *(S->top) = e;
       S->top ++;//栈顶指针+1
       return OK;

}
Status StackTraverse(Stack S)
{
    Stack p;
    if(NULL == S.base )
        return ERROR;
    if(S.base == S.top)
        printf("这是空栈\n");    
    p.top = S.top;
    while(p.top > S.base)
    {
       p.top--;
       printf("%d",*(p.top));
    }
    return OK;

}
int PopStack(Stack* S)
{
    ElemType e;
    if(S->top == S->base)
    {
        return ERROR;
    }
    else
    {       
        S->top--;
        e = *(S->top);//说明:元素并没有删除,仍然在S->top中,但如果插入元素,就会被更新,就像是删除了一样
        return e;
    } 
}
Status GetStackTop(Stack S )
{
    ElemType e;
    if(S.top - S.base >= S.stacksize)
    {
        printf("这是空栈\n");
        return ERROR;
    }

    else
    {
        e = *(S.top-1);
        return e;
    }

}
int StackLength(Stack S)
{
    if(S.base == S.top)
    {
      return OK;
    }
    return S.top - S.base;

}
Status StackEmpty(Stack S)
{
    if(S.base == S.top)
    {
      return ERROR;
    }
    else
      return OK;

}
Status ClearStack(Stack* S)
{
    S->top = S->base;
    return OK;
}
Status MatchBrackets(Stack* S, char * str)
{
    int i = 0; 
    int flag = 0;
    ElemType e = 0;
    while(str[i] != '\0')
    {
       switch(str[i])
       {
           case '{':PushStack(S ,str[i]);              
           break;
           case '[':PushStack(S ,str[i]);
           break;
           case '(':PushStack(S ,str[i]);
           break;
           case '}':
               {                 
                  if( PopStack(S)!= '{')
                  {
                   flag++;
                  }            
               }
           break;
           case ']':
               {
                 if( PopStack(S)!= '[')
                  {
                   flag = 1;
                  } 
               }
           break;
           case ')':
               {
                 if( PopStack(S)!= '(')
                  {
                   flag = 1;
                  } 
               }
           break;
           default:
           break;

       }
      if(flag)
      break;
      i++;
    }
    if(!flag && StackEmpty(*S))
        printf("括号匹配成功\n");
    else if (flag && !StackEmpty(*S))
        printf("括号次序匹配不正确\n");
    else if(flag && StackEmpty(*S))
        printf("右括号多于左括号\n");
    else if(!flag && !StackEmpty(*S))
        printf("左括号多于右括号\n");

    return OK;

}

test.c

#include "MatchBrackets.h"
#include <stdio.h>

void test()
{
    Stack stack;
    char ch1[] = "(())abc{[(])}";
    char ch2[] = "(()))abc{[]}";
    char ch3[] = "(()()abc{[]}";
    char ch4[] = "(())abc{[]()}";
    char *p;
    p = ch4;
    InitStack(&stack);
    MatchBrackets(&stack,p);




}
int main()
{
    test();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/dream8834/article/details/82230398