Chinaunix首页 | 论坛 | 博客
  • 博客访问: 48802
  • 博文数量: 18
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 12
  • 用 户 组: 普通用户
  • 注册时间: 2018-04-03 10:12
文章分类
文章存档

2018年(18)

我的朋友

分类: C/C++

2018-07-27 15:45:32

原文地址:用栈实现括号匹配 作者:sunjiangang-ok

最近在复习数据结构,有关栈的一个问题:括号匹配。
问题描述:输入一串括号,然后判断左右括号是不是匹配的。
实验程序:
/*brackets_match.c*/
  1. #include <stdio.h>

  2. #define M 100

  3. typedef struct Stack {
  4.     char element[M];
  5.     int top;
  6. }Stack;

  7. void InitStack(Stack *s)
  8. {
  9.     s->top = -1;
  10. }

  11. int PushStack(Stack *s, char ch)
  12. {
  13.     if(s->top == M-1) {
  14.         return 0;
  15.     }
  16.     s->top++;
  17.     s->element[s->top] = ch;
  18.     return 1;
  19. }

  20. int PopStack(Stack *s, char *ch)
  21. {
  22.     if(s->top == -1) {
  23.         return 0;
  24.     }
  25.     *ch = s->element[s->top];
  26.     s->top--;
  27.     return 1;
  28. }

  29. int GetTop(Stack *s, char *ch)
  30. {
  31.     if(s->top == -1) {
  32.         return 0;
  33.     }
  34.     *ch = s->element[s->top];
  35.     return 1;
  36. }

  37. int IsEmpty(Stack *s)
  38. {
  39.     if(s->top == -1) {
  40.         return 1;
  41.     } else {
  42.         return 0;
  43.     }
  44. }

  45. int MatchBracket(char ch1, char ch2)
  46. {
  47.     if(ch1 == '(' && ch2 == ')') {
  48.         return 1;
  49.     }
  50.     if(ch1 == '[' && ch2 == ']') {
  51.         return 1;
  52.     }
  53.     if(ch1 == '{' && ch2 == '}') {
  54.         return 1;
  55.     }
  56.     return 0;
  57. }

  58. int main()
  59. {
  60.     Stack s;
  61.     char str[M] = {0}, ch;
  62.     int i;

  63.     InitStack(&s);
  64.     fprintf(stdout, "Input brackets:");
  65.     fscanf(stdin, "%s", str);
  66.     for(i = 0; str[i] != 0; i++) {
  67.         switch(str[i]) {
  68.             case '(':
  69.             case '[':
  70.             case '{':
  71.                 PushStack(&s, str[i]);
  72.                 break;
  73.             case ')':
  74.             case ']':
  75.             case '}':
  76.                 if(IsEmpty(&s)) {
  77.                     printf("right bracket spilth.\n");
  78.                     return 0;
  79.                 } else {
  80.                     GetTop(&s, &ch);
  81.                     if(MatchBracket(ch , str[i])) {
  82.                         PopStack(&s, &ch);
  83.                     } else{
  84.                         printf("left and right brackets are different.\n");
  85.                         return 0;
  86.                     }
  87.                 }
  88.         }
  89.     }
  90.     if(IsEmpty(&s)) {
  91.         printf("left and right brackets are matched.\n");
  92.     } else {    
  93.         printf("left bracket spilth..\n");
  94.     }
  95.     return 0;
  96. }
实验结果:
  1. ^_^[sunny@sunny-laptop ~/DS]10$ gcc brackets_match.c
  2. ^_^[sunny@sunny-laptop ~/DS]11$ ./a.out
  3. Input brackets:[]
  4. left and right brackets are matched.
  5. ^_^[sunny@sunny-laptop ~/DS]12$ ./a.out
  6. Input brackets:{}{}
  7. left and right brackets are matched.
  8. ^_^[sunny@sunny-laptop ~/DS]13$ ./a.out
  9. Input brackets:{{{{{}}}}}}()
  10. right bracket spilth.
  11. ^_^[sunny@sunny-laptop ~/DS]14$ ./a.out
  12. Input brackets:{{{}}}()[]
  13. left and right brackets are matched.

阅读(1952) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~