数据结构(八) 用顺序栈解决括号匹配的问题
来源:互联网 发布:java中反射的应用 编辑:程序博客网 时间:2024/05/16 04:56
//用栈括号匹配的问题
//采用的存储结构为顺序存储结构
#include <iostream>
using namespace std;
#define MAXSIZE 100
//栈的结构体
struct Node
{
int *base;
int *top;
int stackSize;
};
//初始化栈的操作
void initStack(struct Node &S)
{
S.base = new int [MAXSIZE];
if(S.base == NULL)
{
cout<<"地址分配失败\n";
exit(1);
}
S.top = S.base;
S.stackSize = MAXSIZE;
}
//入栈操作
void push(struct Node &S,int e)
{
if(S.top-S.base == S.stackSize)
{
cout<<"此栈已经满了\n";
exit(1);
}
*S.top++ = e;
}
//出栈操作
void pop(struct Node &S,int &e)
{
if(S.top==S.base)
{
//cout<<"栈为空\n";
cout<<"不能匹配\n";
exit(1);
}
e = *--S.top;
}
void function(struct Node S)
{
char c;
cout<<"输入你要匹配的括号(以#作为结束符)\n";
cin>>c;
int e;
while(c!='#')
{
if(c=='<'||c=='('||c=='['||c=='{')
{
push(S,c);
}
else if(c=='>')
{
pop(S,e);
if(e!='<')
{
cout<<"不能匹配\n";
exit(1);
}
}
else if(c==']')
{
pop(S,e);
if(e!='[')
{
cout<<"不能匹配\n";
exit(1);
}
}
else if(c=='}')
{
pop(S,e);
if(e!='{')
{
cout<<"不能匹配\n";
exit(1);
}
}
else if(c==')')
{
pop(S,e);
if(e!='(')
{
cout<<"不能匹配\n";
exit(1);
}
}
cin>>c;
}
if(S.top==S.base)
{
cout<<"能够匹配\n";
}
else
{
cout<<"不能匹配\n";
}
}
int main()
{
struct Node S;
initStack(S);
function(S);
return 0;
}
//采用的存储结构为顺序存储结构
#include <iostream>
using namespace std;
#define MAXSIZE 100
//栈的结构体
struct Node
{
int *base;
int *top;
int stackSize;
};
//初始化栈的操作
void initStack(struct Node &S)
{
S.base = new int [MAXSIZE];
if(S.base == NULL)
{
cout<<"地址分配失败\n";
exit(1);
}
S.top = S.base;
S.stackSize = MAXSIZE;
}
//入栈操作
void push(struct Node &S,int e)
{
if(S.top-S.base == S.stackSize)
{
cout<<"此栈已经满了\n";
exit(1);
}
*S.top++ = e;
}
//出栈操作
void pop(struct Node &S,int &e)
{
if(S.top==S.base)
{
//cout<<"栈为空\n";
cout<<"不能匹配\n";
exit(1);
}
e = *--S.top;
}
void function(struct Node S)
{
char c;
cout<<"输入你要匹配的括号(以#作为结束符)\n";
cin>>c;
int e;
while(c!='#')
{
if(c=='<'||c=='('||c=='['||c=='{')
{
push(S,c);
}
else if(c=='>')
{
pop(S,e);
if(e!='<')
{
cout<<"不能匹配\n";
exit(1);
}
}
else if(c==']')
{
pop(S,e);
if(e!='[')
{
cout<<"不能匹配\n";
exit(1);
}
}
else if(c=='}')
{
pop(S,e);
if(e!='{')
{
cout<<"不能匹配\n";
exit(1);
}
}
else if(c==')')
{
pop(S,e);
if(e!='(')
{
cout<<"不能匹配\n";
exit(1);
}
}
cin>>c;
}
if(S.top==S.base)
{
cout<<"能够匹配\n";
}
else
{
cout<<"不能匹配\n";
}
}
int main()
{
struct Node S;
initStack(S);
function(S);
return 0;
}
0 0
- 数据结构(八) 用顺序栈解决括号匹配的问题
- 数据结构(严蔚敏)顺序栈_括号匹配的检验
- 用C++链式栈解决数据结构中的括号匹配问题。
- 用顺序栈解决括号是否匹配
- Java 用栈解决括号匹配问题
- 数据结构(1)——顺序栈的实现,以及括号匹配的应用
- 数据结构之栈的应用:括号匹配问题
- (C++)数据结构之括号匹配的问题?
- 数据结构 括号匹配问题 (栈 C语言)
- 【数据结构】 利用栈求解 括号匹配问题
- 数据结构-----栈实现括号匹配问题
- 数据结构作业 栈 实现 括号匹配 问题
- 【数据结构_栈_Stack_0962】括号匹配问题
- 数据结构之栈之括号匹配问题
- 8、数据结构笔记之八栈的应用之括号匹配检验实现
- 关于使用栈解决括号匹配的问题
- 顺序表c++数据结构实验之栈四:括号匹配
- C/C++数据结构通过顺序栈实现括号()匹配算法
- 二维数组经典地址问题
- asp页面之间传递值得方法
- Leetcode 73. Set Matrix Zeroes
- 关于cocos2dxActivity监听不到返回健的问题
- 使用xutils请求网络与子线程不同步(子线程报空了),怎么办啊=。=
- 数据结构(八) 用顺序栈解决括号匹配的问题
- 【多媒体性能优化】图片压缩技术探究与实践 更新中
- Android 下载后的视频,有些系统扫描不出来,怎么办?做Android开发
- js获取当前时间和一星期前的时间
- 调用Android系统安装的软件打开文件,程序停止运行
- 怎么把修改APK把它变成自己的APK只想改个引导页然后要可以运行
- ios other linker flag 问题
- Ananagrams UVA 156(反片语)
- PHP-FPM pm.max_children设置依据