實驗五:數據結構之順序棧 實踐 順序棧實現括號匹配檢測

實驗結果如下:

實驗代碼如下:

完整源碼:

Stack.h

  1 #include <stdio.h>
  2 #include <malloc.h>
  3 #define OK 0                        //成功執行
  4 #define Err_Memory -1               //內存分配錯誤 
  5 #define Err_InvalidParam -2         //輸入參數無效
  6 #define Err_Overflow -3             //溢出錯誤
  7 #define Err_IllegalPos -4           //非法位置
  8 #define Err_NoResult -5             //無法返回結果或返回結果爲空
  9 #define Stack_Init_Size 100              //順序表最大長度
 10 #define Stack_Increment 10         //順序表存儲空間分配增量
 11 
 12 typedef char ElemType;                          //定義順序表的元素類型ElemType
 13 
 14 typedef struct {
 15     ElemType * stackdata;           //data指向存儲數據元素的一維數組,初始大小爲Max_Length
 16     int top;                        //順序表的實際長度,其值小於等於ListLength
 17     int stacksize;                  //當前順序表分配的空間大小,初始值爲Max_Length
 18 } SeqStack;                         //順序表結構定義
 19 
 20 typedef int Status;                 //定義返回狀態
 21 
 22 Status InitStack(SeqStack *S);
 23 Status EmptyStack(SeqStack *S);
 24 Status ClearStack(SeqStack *S);
 25 void TraverseStack(SeqStack *S);
 26 Status LengthStack(SeqStack *S);
 27 Status GetTop(SeqStack *S, ElemType *e);
 28 Status Push(SeqStack *S, ElemType e);
 29 Status Pop(SeqStack *S, ElemType *e);


Stack.c

 

 1 #include "Stack.h"
  2 
  3 
  4 Status InitStack(SeqStack *S)
  5 {
  6     if(!S)
  7         return Err_InvalidParam;
  8 
  9     S->stackdata = (ElemType *)malloc(Stack_Increment*sizeof(ElemType));
 10 
 11     if(!S->stackdata)
 12         return Err_Memory;
 13 
 14     S->top = 0;
 15     S->stacksize = Stack_Init_Size;
 16 
 17     return OK;
 18 }
 19 
 20 Status ClearStack(SeqStack *S)
 21 {
 22     if(!S)
 23         return Err_InvalidParam;
 24 
 25     S->top = 0;
 26 
 27     return 0;
 28 }
 29 
 30 Status EmptyStack(SeqStack *S)
 31 {
 32     if(!S)
 33         return Err_InvalidParam;
 34     return (S->top == 0);
 35 }
 36 
 37 Status LengthStack(SeqStack *S)
 38 {
 39     return S->top;
 40 }
 41 
 42 void TraverseStack(SeqStack *S)
 43 {
 44     int i;
 45 
 46     for(i = 0; i<S->top; i++)
 47       printf("%d\n", *S->stackdata);
 48 }
 49 
 50 Status GetTop(SeqStack *S, ElemType *e)
 51 {
 52     if(!S)
 53         return Err_InvalidParam;
 54 
 55     if(S->top == 0)
 56         return Err_NoResult;
 57 
 58     *e = S->stackdata[S->top-1];
 59 
 60     return 0;
 61 }
 62 
 63 Status Push(SeqStack *S, ElemType e)
 64 {
 65     ElemType *newstack;
 66 
 67     if(!S)
 68         return Err_InvalidParam;
 69 
 70     if(S->top == S->stacksize)
 71         newstack = (ElemType *)realloc(S->stackdata, (S->stacksize+Stack_Increment)*sizeof(ElemType));
 72 
 73     if(!newstack)
 74         return Err_Memory;
 75 
 76 //  S->top = S->stackdata + S->stacksize; //???????????
 77     S->stacksize += Stack_Increment;
 78     S->stackdata[S->top++] = e;
 79 
 80     return OK;
 81 }
 82 
 83 Status Pop(SeqStack *S, ElemType *e)
 84 {
 85 
 86     if(!S)
 87         return Err_InvalidParam;
 88 
 89     if(S->top == 0)
 90         return Err_NoResult;
 91 
 92     *e = S->stackdata[--S->top];
 93 
 94     return OK;
 95 }


括號匹配文件:Backets_match.c

  1 #include <stdio.h>
  2 #include "Stack.h"
  3 
  4 void brackets(char *expression)
  5 {
  6     SeqStack *S;
  7     char ch1, ch2;
  8     int pos = 1;
  9 
 10     S = (SeqStack *)malloc(sizeof(SeqStack));
 11     if(!S){
 12         printf("內存分配錯誤\n");
 13         return;
 14     }
 15     InitStack(S);
 16     ch1 = expression[pos-1];
 17     while(ch1 != '\0'){
 18         if(ch1 == '(' || ch1 == '[' || ch1 == '{'){
 19             Push(S, ch1);
 20         } else if(ch1 == ')' || ch1 == ']' || ch1 == '}'){
 21             if(EmptyStack(S)){
 22                 printf("表達式中第%d個位置的括號‘%c’不匹配!\n", pos, ch1);
 23                 return;
 24             } else {
 25                 Pop(S, &ch2);
 26                 if((ch1 == ')' && ch2 != '(') || (ch1 == ']' && ch2 != '[') || (ch1 == '}' && ch2 != '{')){
 27                     printf("表達式中第%d個位置的括號‘%c’不匹配!\n", pos, ch1);
 28                     return;
 29                 }
 30 
 31             }
 32         }
 33         ch1 = expression[pos++];
 34 
 35     }
 36     if(EmptyStack(S))
 37       printf("括號匹配\n");
 38     else
 39       printf("表達式中缺少右括號!\n");
 40 
 41 }
 42 int main()
 43 {
 44     char str[100];
 45 
 46     printf("請輸入表達式(不超過100個字符):\n");
 47     
 48     gets(str);
 49     brackets(str);
 50 
 51     return 0;
 52 }

編譯命令:gcc Stack.c Brackets_match.c

執行命令:./a.out

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章