用栈实现括号匹配
来源:互联网 发布:天谕萨菲罗斯捏脸数据 编辑:程序博客网 时间:2024/05/20 08:42
最近在复习数据结构,有关栈的一个问题:括号匹配。
问题描述:输入一串括号,然后判断左右括号是不是匹配的。
实验程序:
/*brackets_match.c*/
- #include <stdio.h>
- #define M 100
- typedef struct Stack {
- char element[M];
- int top;
- }Stack;
- void InitStack(Stack *s)
- {
- s->top = -1;
- }
- int PushStack(Stack *s, char ch)
- {
- if(s->top == M-1) {
- return 0;
- }
- s->top++;
- s->element[s->top] = ch;
- return 1;
- }
- int PopStack(Stack *s, char *ch)
- {
- if(s->top == -1) {
- return 0;
- }
- *ch = s->element[s->top];
- s->top--;
- return 1;
- }
- int GetTop(Stack *s, char *ch)
- {
- if(s->top == -1) {
- return 0;
- }
- *ch = s->element[s->top];
- return 1;
- }
- int IsEmpty(Stack *s)
- {
- if(s->top == -1) {
- return 1;
- } else {
- return 0;
- }
- }
- int MatchBracket(char ch1, char ch2)
- {
- if(ch1 == '(' && ch2 == ')') {
- return 1;
- }
- if(ch1 == '[' && ch2 == ']') {
- return 1;
- }
- if(ch1 == '{' && ch2 == '}') {
- return 1;
- }
- return 0;
- }
- int main()
- {
- Stack s;
- char str[M] = {0}, ch;
- int i;
- InitStack(&s);
- fprintf(stdout, "Input brackets:");
- fscanf(stdin, "%s", str);
- for(i = 0; str[i] != 0; i++) {
- switch(str[i]) {
- case '(':
- case '[':
- case '{':
- PushStack(&s, str[i]);
- break;
- case ')':
- case ']':
- case '}':
- if(IsEmpty(&s)) {
- printf("right bracket spilth.\n");
- return 0;
- } else {
- GetTop(&s, &ch);
- if(MatchBracket(ch , str[i])) {
- PopStack(&s, &ch);
- } else{
- printf("left and right brackets are different.\n");
- return 0;
- }
- }
- }
- }
- if(IsEmpty(&s)) {
- printf("left and right brackets are matched.\n");
- } else {
- printf("left bracket spilth..\n");
- }
- return 0;
- }
实验结果:
- ^_^[sunny@sunny-laptop ~/DS]10$ gcc brackets_match.c
- ^_^[sunny@sunny-laptop ~/DS]11$ ./a.out
- Input brackets:[]
- left and right brackets are matched.
- ^_^[sunny@sunny-laptop ~/DS]12$ ./a.out
- Input brackets:{}{}
- left and right brackets are matched.
- ^_^[sunny@sunny-laptop ~/DS]13$ ./a.out
- Input brackets:{{{{{}}}}}}()
- right bracket spilth.
- ^_^[sunny@sunny-laptop ~/DS]14$ ./a.out
- Input brackets:{{{}}}()[]
- left and right brackets are matched.
0
上一篇:linux内核list.h头文件分析(四)
下一篇:linux内核list.h头文件分析(五)——hlist分析
相关热门文章
- MyBatis 入门(五)--分页查询(...
- linux设备驱动归纳总结(三)...
- Linux内核驱动fsync机制实现图...
- Linux-千兆网卡驱动实现机制浅...
- 即时通信的app,服务器端需要的...
- test123
- 编写安全代码——小心有符号数...
- 彻底搞定C语言指针详解-完整版...
- 使用openssl api进行加密解密...
- 一段自己打印自己的c程序...
- linux dhcp peizhi roc
- 关于Unix文件的软链接
- 求教这个命令什么意思,我是新...
- sed -e "/grep/d" 是什么意思...
- 谁能够帮我解决LINUX 2.6 10...
给主人留下些什么吧!~~
评论热议
0 0
- 用栈实现括号匹配
- 用栈实现括号匹配
- 用栈实现括号匹配
- 栈实现括号匹配
- 栈实现括号匹配
- 栈实现括号匹配
- 栈实现括号匹配
- 括号匹配---栈实现
- 栈实现括号匹配
- 栈实现括号匹配
- 括号匹配,栈实现
- 栈实现括号匹配
- 用顺序栈实现匹配括号功能
- 用栈实现括号匹配的检验
- 用栈实现括号匹配的检验
- 用栈实现括号匹配的检验
- 括号匹配问题,用栈实现
- 用栈实现括号匹配的算法
- linux内核list.h头文件分析(三)
- java关于正则相关的函数的应用--compile(),match(),find(),group(),
- 你知道精通自动化测试有多重要吗?
- linux内核list.h头文件分析(四)
- 时序分析/约束——Xilinx时钟资源 & ISE时序分析器
- 用栈实现括号匹配
- SylixOS MII总线层解析
- 2017第八届蓝桥杯C/C++ B组省赛题解
- 前端面试三
- Ubuntu SSH Algorithm negotiation failed
- linux内核list.h头文件分析(五)——hlist分析
- Fragment与Activity交互方式使用Bundle
- linux内核list.h头文件分析(六)——hlist分析
- NBUT1218 You are my brother(弱化版带权并查集)
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
还原剂和氧化剂
铝抗氧化剂
氢氧化钠干燥剂
氧化锌脱硫剂价格
氧化漂白剂
氧化剂是什么
什么是氧化剂什么是还原剂
去氧化剂
固体氧化剂
弱氧化剂
天然氧化剂
铝合金防氧化剂
新型氧化剂
不锈钢氧化剂
脱氧化剂
高温防氧化剂
氧化剂物品
无铬氧化剂
铜氧化剂
绿色氧化剂
塑料氧化剂
铝合金氧化剂
氧化剂检测
一级氧化剂
活性氧化剂
金属防氧化剂
氧化剂的定义
哪些是氧化剂
二级氧化剂
促氧化剂
氧化剂定义
氧化剂被还原
无色强氧化剂
氧化剂和有机过氧化物
氧化剂具有还原性
氧化剂有氧化性
氧化剂发生还原反应
氢氧化钠是氧化剂吗
常见氧化剂
臭氧是一种强氧化剂
氧化态和还原态的定义