重学数据结构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;  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值