데이터 구조 재 미 있 는 문제 - 괄호 일치

10889 단어 데이터 구조
   1: #include <stdio.h>
   2: #include <stdlib.h>
   3: #define STACK_INIT_SIZE 20
   4: #define STACKINCREMENT 10
   5:  
   6: typedef char ElemType;   /* char     ElemType*/
   7:  
   8: typedef struct{        /*       */
   9:     ElemType *base;
  10:     ElemType *top;
  11:     int stacksize;
  12: }sqStack;
  13:  
  14:  
  15: void initStack(sqStack *s)
  16: {
  17:     /*                ,      s->base*/
  18:     s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
  19:     if(!s->base) exit(0);     /*      */
  20:     s->top = s->base;       /*   ,      */
  21:     s->stacksize = STACK_INIT_SIZE;   /*     STACK_INIT_SIZE */
  22: }
  23:  
  24: void Push(sqStack *s, ElemType e){        /*    */
  25:     if(s->top - s->base >= s->stacksize){
  26:     /*  ,    */
  27:     s->base = (ElemType *)realloc(s->base, (s->stacksize +
  28:     STACKINCREMENT)*sizeof(ElemType));
  29:     if(!s->base) exit(0);   /*      */
  30:     s->top = s->base + s->stacksize;
  31:     s->stacksize = s->stacksize + STACKINCREMENT; /*        */
  32:     }
  33:     *(s->top) = e;  /*    */
  34:         s->top++;
  35: }
  36:  
  37: void Pop(sqStack *s , ElemType *e){   /*    */
  38:     if(s->top == s->base) return;  /*       */
  39:     *e = *--(s->top);              /*      */
  40: }
  41:  
  42: int StackLen(sqStack s){     /*   s   */
  43:     return (s.top - s.base) ;
  44: }
  45:  
  46:  
  47: int match(char e,char c){
  48:     if(e=='(' && c==')')return 1;
  49:     if(e=='[' && c==']')return 1;
  50:     return 0;
  51: }
  52:  
  53: int main()
  54: {
  55:     sqStack s;
  56:     char c , e ;
  57:     initStack( &s ) ;  /*       */
  58:     scanf("%c",&c);  /*       */
  59:     while(c!='#'){   /*'#'        */
  60:         if(!StackLen(s))
  61:             Push(&s,c);  /*     ,            ,       */
  62:         else
  63:         {
  64:              Pop(&s,&e);    /*      */
  65:              if(!match(e,c)){  /*                ,       */
  66:                 Push(&s,e);   /*           */
  67:                 Push(&s,c);   /*           */
  68:                }
  69:         }
  70:         scanf("%c",&c);  /*       */
  71:         }
  72:     if(!StackLen(s))  printf("The brackets are matched
"
); /* s , */
  73:     else  printf("The brackets are not matched
"
); /* s , */
  74:     return 0;
  75:  
  76: }
  77:  

좋은 웹페이지 즐겨찾기