当前位置:网站首页>栈的经典应用—括号匹配问题
栈的经典应用—括号匹配问题
2022-07-06 09:27:00 【帅帅气气的黑猫警长】
括号匹配问题算是栈应用中比较经典的问题了。
给定一个只包括 (,),{ ,},[,] 的字符串,判断字符串是否有效。
有效字符串需满足:
1、左括号必须用相同类型的右括号闭合。
2、左括号必须以正确的顺序闭合。
注意空字符串可被认为是有效字符串。
顺序栈核心代码和算法
typedef struct { //顺序栈结构体
char data[MAXSIZE];
int top;
}sqstack;
void initstack(sqstack &s) //初始化顺序栈
bool push(sqstack &s,char e) //push操作
bool pop(sqstack &s,char &e) //pop操作
bool stackempty(sqstack s) //判空
bool chack(char str[],int length) //匹配代码
{
sqstack s;
initstack(s);
cout<<"initstack"<<endl;
for(int i=0;i<length;i++)
{
//cout<<str[i]<<endl;
if(str[i]=='(' ||str[i]=='[' ||str[i]=='{') //把(,[,{左半部分push到栈
{
push(s,str[i]);
}
else {
if(stackempty(s)) //先判断栈中还有没有待配对元素
return 0;
char topelem;
pop(s,topelem); //pop出栈顶元素与之配对
//cout<<topelem<<" "<<str[i]<<endl;
if(str[i]==')'&&topelem!='(')
return 0;
if(str[i]==']'&&topelem!='[')
return 0;
if(str[i]=='}'&&topelem!='{')
return 0;
}
}
return stackempty(s); //最后匹配完看栈中有无剩余
}顺序栈实现完整代码
#include <iostream>
#define MAXSIZE 20
using namespace std;
typedef struct {
char data[MAXSIZE];
int top;
}sqstack;
void initstack(sqstack &s) //初始化
{
s.top=-1;
}
bool push(sqstack &s,char e) //push操作
{
if(s.top==MAXSIZE-1)
return 0;
s.top=s.top+1;
s.data[s.top]=e;
return 1;
}
bool pop(sqstack &s,char &e) //pop操作
{
if(s.top==-1)
return 0;
e=s.data[s.top];
s.top=s.top-1;
return 1;
}
bool stackempty(sqstack s) //判空操作
{
if(s.top==-1)
return 1;
else return 0;
}
bool chack(char str[],int length) //匹配操作
{
sqstack s;
initstack(s);
cout<<"initstack"<<endl;
for(int i=0;i<length;i++)
{
//cout<<str[i]<<endl;
if(str[i]=='(' ||str[i]=='[' ||str[i]=='{')
{
push(s,str[i]);
}
else {
if(stackempty(s))
return 0;
char topelem;
pop(s,topelem);
//cout<<topelem<<" "<<str[i]<<endl;
if(str[i]==')'&&topelem!='(')
return 0;
if(str[i]==']'&&topelem!='[')
return 0;
if(str[i]=='}'&&topelem!='{')
return 0;
}
}
return stackempty(s);
}
int main() {
char str[]={'{','[','(',')','(',')',']','}'};
int length=sizeof(str)/sizeof(str[0]); //此处为判断str数组长度
cout<<length<<endl;
if(chack(str,length))
cout<<"yes"<<endl;
else cout<<"no"<<endl;
cout <<"over\n";
return 0;
}链栈实现完整代码
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef struct lnode{ //链栈结构体
char data;
struct lnode *next;
}lnode,*linkstack;
bool initstack(linkstack &s) //初始化链栈(表)
{
s=(lnode*)malloc(sizeof(lnode));
if(s==NULL)
return 0;
s->next=NULL;
return 1;
}
bool push(linkstack &s,char e) //push(类似链表头插)
{
lnode *p;
p=(lnode*)malloc(sizeof(lnode));
if(p==NULL)
return 0;
p->data=e;
p->next=s->next;
s->next=p;
return 1;
}
bool pop(linkstack &s,char &e) //pop(类似链表删除)
{
if(s->next==NULL)
return 0;
lnode *p;
p=s->next;
e=s->next->data;
s->next=p->next;
free(p);
return 1;
}
bool stackempty(linkstack s) //判空
{
if(s->next==NULL)
return 1;
else return 0;
}
bool chack(char str[],int length) //匹配算法
{
linkstack s;
initstack(s);
cout<<"initstack"<<endl;
for(int i=0;i<length;i++)
{
//cout<<str[i]<<endl;
if(str[i]=='(' ||str[i]=='[' ||str[i]=='{')
{
push(s,str[i]);
}
else {
if(stackempty(s))
return 0;
char topelem;
pop(s,topelem);
cout<<topelem<<" "<<str[i]<<endl;
if(str[i]==')'&&topelem!='(')
return 0;
if(str[i]==']'&&topelem!='[')
return 0;
if(str[i]=='}'&&topelem!='{')
return 0;
}
}
return stackempty(s);
}
int main() {
char str[]={'{','[','(',')','(',')',']','}'};
int length=sizeof(str)/sizeof(str[0]);
cout<<length<<endl;
if(chack(str,length))
cout<<"yes"<<endl;
else cout<<"no"<<endl;
cout <<"over\n";
return 0;
}
边栏推荐
- 基于web的照片数码冲印网站
- nodejs爬虫
- 【练习-10】 Unread Messages(未读消息)
- The most complete programming language online API document
- Interesting drink
- Information security - threat detection engine - common rule engine base performance comparison
- Research Report of exterior wall insulation system (ewis) industry - market status analysis and development prospect prediction
- STM32 how to use stlink download program: light LED running light (Library version)
- Nodejs+vue online fresh flower shop sales information system express+mysql
- Opencv learning log 31 -- background difference
猜你喜欢
frida hook so层、protobuf 数据解析

X-forwarded-for details, how to get the client IP

数据在内存中的存储&载入内存,让程序运行起来

Matlab comprehensive exercise: application in signal and system

Determine the Photo Position

【练习-4】(Uva 11988)Broken Keyboard(破损的键盘) ==(链表)

【高老师UML软件建模基础】20级云班课习题答案合集

Penetration test (8) -- official document of burp Suite Pro

Information security - Analysis of security orchestration automation and response (soar) technology

Differential (one-dimensional, two-dimensional, three-dimensional) Blue Bridge Cup three body attack
随机推荐
Ball Dropping
CS zero foundation introductory learning record
Pyside6 signal, slot
Hdu-6025-prime sequence (girls' competition)
mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’
差分(一维,二维,三维) 蓝桥杯三体攻击
【练习-7】(Uva 10976)Fractions Again?!(分数拆分)
Opencv learning log 19 skin grinding
X-Forwarded-For详解、如何获取到客户端IP
[exercise-7] crossover answers
Optimization method of path problem before dynamic planning
洛谷P1102 A-B数对(二分,map,双指针)
Information security - Epic vulnerability log4j vulnerability mechanism and preventive measures
b站 实时弹幕和历史弹幕 Protobuf 格式解析
New to redis
Basic Q & A of introductory C language
D - function (HDU - 6546) girls' competition
Opencv learning log 16 paperclip count
Opencv learning log 13 corrosion, expansion, opening and closing operations
【练习-1】(Uva 673) Parentheses Balance/平衡的括号 (栈stack)