• Stack栈——application of stack


    判断括号表达式是否平衡

    1. 平衡:example:{[]}()
    2. 不平衡:example:{[}](

    判断要push的是否为括号的开始符号,也就是'(' '{' '['。如果是则push栈中,反之若是')' '}' ']',则判断栈是否为空,如果是空直接返回 no balance,若不为空c = pop栈,并与c进行匹配,匹配成功,就进行下一次的插入,反之 返回 no balance。

      1 #include<stdio.h>
      2 #include<stdlib.h>
      3 
      4 struct stack
      5 {
      6     int data;
      7     struct stack * next;
      8 };
      9 int matchParenthese(int c1, int c2)
     10 {
     11     if('(' == c1 && ')' == c2)
     12     {
     13         return 1;
     14     }
     15     else if('[' == c1 && ']' == c2)
     16     {
     17         return 1;
     18     }
     19     else if('{' == c1 && '}' == c2)
     20     {
     21         return 1;
     22     }
     23     else
     24     {
     25         return 0;
     26     }
     27 }
     28 int judgeParenthese(char ex[])
     29 {
     30     struct stack * s = NULL;
     31     int index = 0;
     32 
     33     while(ex[index])
     34     {
     35         if('(' == ex[index] || '[' == ex[index] || '{' == ex[index])
     36         {
     37             push(&s, ex[index]);
     38         }
     39         else if(')' == ex[index] || ']' == ex[index] || '}' == ex[index])
     40         {
     41             if(NULL == s)
     42             {
     43                 printf("
    The stack is empty");
     44                 return 0;
     45             }
     46             else if(!(matchParenthese(pop(&s), ex[index])))
     47             {
     48                 return 0;
     49             }
     50 
     51         }
     52         index++;
     53     }
     54     if(NULL == s)
     55     {
     56         return 1;
     57     }
     58     else
     59     {
     60         return 0;
     61     }
     62 }
     63 struct stack * newNode(int data)
     64 {
     65     struct stack * new_node = (struct stack *)malloc(sizeof(struct stack));
     66     new_node->data = data;
     67     new_node->next = NULL;
     68 
     69     return new_node;
     70 }
     71 void push(struct stack ** root, int data)
     72 {
     73     struct stack * new_node = newNode(data);
     74 
     75     new_node->next = (*root);
     76     (*root) = (new_node);
     77     printf("
     %c pushed to stack", data);
     78 }
     79 int isEmpty(struct stack * root)
     80 {
     81     return NULL == root ? 1 : 0;
     82 }
     83 int pop(struct stack ** root)
     84 {
     85     if(isEmpty((*root)))
     86     {
     87         printf("
    UnderFlow");
     88         return 0;
     89     }
     90     struct stack * temp = (*root);
     91     int data = temp->data;
     92     (*root) = temp->next;
     93     free(temp);
     94     printf("
     The %c poped of stack", data);
     95     return data;
     96 }
     97 int peek(struct stack * root)
     98 {
     99     if(isEmpty(root))
    100     {
    101         printf("
    UnderFlow");
    102         return 0;
    103     }
    104     return root->data;
    105 }
    106 int main(void)
    107 {
    108     char ex[] = "{()}[";
    109     if(judgeParenthese(ex))
    110     {
    111         printf("
    Balance!");
    112     }
    113     else
    114     {
    115         printf("
    NO!");
    116     }
    117     return 0;
    118 }
  • 相关阅读:
    添加依赖到pom.xml
    关于换了手机后,导致原来连的fiddler抓不到新手机上的包的解决方法
    关于无法使用python执行进入百度页面的代码修改
    安装完jdk配置环境变量
    关于解决工作中的自动化环境搭建的解决方案(序)
    关于微信公众号的测试
    关于发布中报“未能加载文件或程序集“Newtonsoft.Json”或它的某一个依赖项”的问题解决方法
    .Net Native 跨平台尝试
    ASP.NET 5 Beta8 已经发布
    go框架beego Windows 搭建记录和遇到的坑
  • 原文地址:https://www.cnblogs.com/AI-Cobe/p/9358879.html
Copyright © 2020-2023  润新知