重學數據結構005——棧的應用之平衡符號

        之前學習了棧的基本操作,並且學習了棧的兩種實現方式:鏈式存儲和順序存儲(數組)。現在看看棧都有哪些應用。棧的一個主要應用是平衡符號。

        初學者在編寫代碼並且編譯時,難免會因爲少寫了一個')'和被編譯器報錯。也就是說,編譯器會去匹配括號是否匹配。當你輸入了一個'(',很自然編譯器回去檢查你是否有另一個')'符號與之匹配。如果所有的括號都能夠成對出現,那麼編譯器是能夠通過的。否則編譯器會報錯。例如字符序列“(a+b)”是匹配的,而字符序列"(a+b]"則不是。

        在檢測括號匹配的算法中使用到了棧,算法描述如下:創建一個空棧,讀取字符序列直到結尾。如果字符是開放符號'(''[''{',將其入棧;如果是一個封閉符號')'']''}',則當棧爲空時報錯。否則,將棧頂元素彈出。如果彈出的符號不是對應的開放符號,則報錯。當字符序列結束,判斷棧是否爲空,爲空則報錯。

        下面是我的實現代碼:

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #define ElementType char  
  4.  
  5.  
  6. typedef struct Node *PtrToNode;  
  7. typedef PtrToNode Stack;  
  8. typedef struct Node  
  9. {  
  10.     ElementType Element;  
  11.     PtrToNode Next;  
  12. };  
  13.  
  14. int IsEmpty(Stack S);  
  15. Stack CreateStack();  
  16. void DisposeStack(Stack S);  
  17. void MakeEmpty(Stack S);  
  18. void Push(ElementType X,Stack S);  
  19. ElementType Top(Stack S);  
  20. void Pop(Stack S);  
  21.  
  22. //判斷棧是否爲空  
  23. int IsEmpty(Stack S)  
  24. {  
  25.     return S->Next == NULL;  
  26. }  
  27. //創建鏈棧  
  28. Stack CreateStack()  
  29. {  
  30.     Stack S = malloc(sizeof(struct Node));  
  31.     if(S == NULL)  
  32.     {  
  33.         printf("No enough memory!");  
  34.         return NULL;  
  35.     }  
  36.     S->Next = NULL;  
  37.     MakeEmpty(S);  
  38.     return S;  
  39. }  
  40.  
  41. void MakeEmpty(Stack S)  
  42. {  
  43.     if(S == NULL)  
  44.     {  
  45.         printf("Use CreateStack First!");  
  46.     }  
  47.     else 
  48.     {  
  49.         while(!IsEmpty(S))  
  50.         {  
  51.             Pop(S);  
  52.         }  
  53.     }  
  54. }  
  55.  
  56. void Push(ElementType X,Stack S)  
  57. {  
  58.     PtrToNode Tmp;  
  59.     Tmp = malloc(sizeof(struct Node));  
  60.     if(Tmp != NULL)  
  61.     {  
  62.         Tmp->Element = X;  
  63.         Tmp->Next = S->Next;  
  64.         S->Next = Tmp;  
  65.     }  
  66.     else 
  67.     {  
  68.         printf("Out of space!");  
  69.     }  
  70. }  
  71.  
  72. void Pop(Stack S)  
  73. {  
  74.       
  75.     if(IsEmpty(S))  
  76.     {  
  77.         printf("The Stack is Empty!");  
  78.     }  
  79.     else 
  80.     {  
  81.         PtrToNode Tmp = S->Next;  
  82.         S->Next = Tmp->Next;  
  83.         free(Tmp);  
  84.     }  
  85. }  
  86.  
  87. ElementType Top(Stack S)  
  88. {  
  89.     if(IsEmpty(S))  
  90.     {  
  91.         printf("The stack is empty!");  
  92.         return 0;  
  93.     }  
  94.     else 
  95.     {  
  96.         return S->Next->Element;  
  97.     }  
  98. }  
  99.  
  100. //平衡符號判斷  
  101. void balance(char *ch,Stack S)  
  102. {  
  103.     ElementType c;  
  104.     MakeEmpty(S);  
  105.     while((c=*ch) != '\0')  
  106.     {  
  107.         printf("%c\n",c);  
  108.         switch(c)  
  109.         {  
  110.         case '(':  
  111.         case '[':  
  112.         case '{':  
  113.             Push(c,S);  
  114.             break;  
  115.         case ')':  
  116.             if(IsEmpty(S))  
  117.             {  
  118.                 fprintf(stderr,"The symbols not balance!\n");  
  119.                 return;  
  120.             }  
  121.             else 
  122.             {  
  123.                 if(Top(S)=='(')  
  124.                 {  
  125.                     Pop(S);  
  126.                 }  
  127.                 else   
  128.                 {  
  129.                     fprintf(stderr,"The symbols not balance!\n");  
  130.                     return;  
  131.                 }  
  132.             }  
  133.             break;  
  134.         case ']':  
  135.             if(IsEmpty(S))  
  136.             {  
  137.                 fprintf(stderr,"The symbols not balance!\n");  
  138.                 return;  
  139.             }  
  140.             else 
  141.             {  
  142.                 if(Top(S)=='[')  
  143.                 {  
  144.                     Pop(S);  
  145.                 }  
  146.                 else   
  147.                 {  
  148.                     fprintf(stderr,"The symbols not balance!\n");  
  149.                     return;  
  150.                 }  
  151.             }  
  152.             break;  
  153.         case '}':  
  154.             if(IsEmpty(S))  
  155.             {  
  156.                 fprintf(stderr,"The symbols not balance!\n");  
  157.                 return;  
  158.             }  
  159.             else 
  160.             {  
  161.                 if(Top(S)=='{')  
  162.                 {  
  163.                     Pop(S);  
  164.                 }  
  165.                 else   
  166.                 {  
  167.                     fprintf(stderr,"The symbols not balance!\n");  
  168.                     return;  
  169.                 }  
  170.             }  
  171.             break;  
  172.         default:  
  173.             break;  
  174.         }  
  175.         ch++;  
  176.     }  
  177.     if(IsEmpty(S))   
  178.     {  
  179.         fprintf(stdout,"The Symbols Balance!\n");  
  180.     }  
  181.     else 
  182.     {  
  183.         fprintf(stderr,"The Symbols Not Balance!\n");  
  184.     }  
  185. }  
  186.  
  187. int main(void)  
  188. {  
  189.     char ch[] = "(a+b){[d]c*d}{";  
  190.     Stack S = CreateStack();  
  191.     balance(ch,S);  
  192.     return 0;  

 

發佈了51 篇原創文章 · 獲贊 9 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章