[数据结构-栈]栈的基本操作&括号匹配
来源:互联网 发布:windows启动过程 编辑:程序博客网 时间:2024/05/29 06:31
堆栈的三种实现方式
http://www.cnblogs.com/baochuan/archive/2013/04/11/3010437.html
用静态数据实现
#include<stdio.h>#define MAX_SIZE 100int top = -1;int stack[MAX_SIZE];void init(){ top = -1;}int is_empty(){ return top == -1;}int is_full(){ return top == MAX_SIZE - 1;}void pop(){ if(!is_empty()) { top--; } }void push(int value){ if(!is_full()) stack[++top] = value;}void get_top(){ if(is_empty()) printf("E\n"); else printf("%d\n", stack[top]);}int main(){ int n; while(1) { if(scanf("%d", &n) == 1); if(n == 0) break; init(); int i; for(i = 0; i < n; i ++) { char opt; if(scanf(" %c", &opt)); switch(opt) { case 'P': { int value; if(scanf("%d", &value)); push(value); } break; case 'O': pop(); break; case 'A': get_top(); break; default: break; } } printf("\n"); } return 0;}
括号匹配问题,用STL
#include<stdio.h>#include<stack>#include<string.h>using namespace std;stack<int> S;//重点:栈中只存放左括号,一遇到右括号就出栈 char str[110];char ans[110];int main(){ while(scanf("%s", str) != EOF) { int i; for(i = 0; i < strlen(str); i ++) { if(str[i] == '(') { S.push(i); //将其数组下标放到堆栈中 ans[i] = ' '; } else if(str[i] == ')') { if(S.empty()) ans[i] = '?'; else { S.pop(); ans[i] = ' '; } } else ans[i] = ' '; } while(!S.empty()) { ans[S.top()] = '$'; S.pop(); } puts(str); puts(ans); } return 0;}
阅读全文
0 0
- [数据结构-栈]栈的基本操作&括号匹配
- 利用栈的基本操作,检查括号是否匹配
- 数据结构-栈-括号匹配
- 数据结构栈的应用之括号匹配
- 数据结构之 栈的应用 括号匹配
- 数据结构 栈的应用 括号匹配
- 【数据结构】栈的应用 括号匹配
- 栈的应用-括号匹配-数据结构
- 括号匹配,栈的基本应用;
- 数据结构栈应用括号匹配
- 第六周--数据结构--数据结构之括号的匹配(栈)
- 栈的基本操作及如何判断一个表达式中的左右括号是否匹配
- 第六周数据结构实践——括号的匹配(栈)【项目3-括号的匹配】
- 括号匹配(栈操作)
- php 栈操作匹配括号( )
- 【数据结构】栈的应用--括号的匹配(c++)
- 数据结构 P49 栈的应用举例-括号匹配的检验
- 数据结构——链栈(附基本的括号匹配)
- java中IO流中的 打印流
- 染色 HYSBZ
- Mosquitto搭建Android推送服务(三)Mosquitto集群搭建
- 78 C语言函数的递归调用
- 剑指offer:第22题栈的压入、弹出顺序
- [数据结构-栈]栈的基本操作&括号匹配
- Search in Rotated Sorted Array问题及解法
- 为什么说Tcl是最好的语言?
- 【《Real-Time Rendering 3rd》 提炼总结】(四) 第五章 · 图形渲染与视觉外观 The Visual Appearance
- 读Zepto源码之操作DOM
- SweetAlert入门教程
- OSGi简介
- 输入输出
- RAC添加新节点绕过dbca新建实例的方法