//严蔚敏《数据结构》
//栈的应用举例:括号匹配的检验
//自学数据结构中,加油啊!!!
#include<iostream>
using namespace std;
#define ElemType char
typedef struct SNode
{
ElemType data;
struct SNode* prior,* next;
}SNode,* Link;
typedef struct
{
Link head,tail;
int len;
}LinkStack;
bool Init_Stack(LinkStack& s)//初始化链栈
{
s.head=new SNode;
if(!s.head)
return false;
s.head->next=s.head->prior=nullptr;
s.tail=s.head;
s.len=0;
return true;
}
void Output_Stack(LinkStack s)//遍历输出链栈元素
{
Link p=s.tail;//p从栈顶开始遍历
cout<<"栈元素:(从栈顶开始遍历)\n";
cout<<"--------"<<endl;
while(p){
cout<<p->data<<endl;
p=p->prior;
}
cout<<"--------"<<endl;
}
ElemType GetTop_Stack(LinkStack s)
{
return s.tail->data;
}
bool IsEmpty_Stack(LinkStack s)//链栈为空,返回true;否则返回false
{
if(!s.len)
return true;
return false;
}
bool Push_Stack(LinkStack& s,ElemType e)//入栈
{
Link p=new SNode;
if(!p)
return false;
p->data=e;
p->next=s.tail->next;
s.tail->next=p;
p->prior=s.tail;
s.tail=p;
s.len++;
return true;
}
bool Pop_Stack(LinkStack& s,ElemType& e)//出栈
{
Link p=s.tail->prior;
e=s.tail->data;
free(s.tail);
s.tail=p;
s.len--;
return true;
}
bool Match(char ch1,ElemType ch2)//如果括号匹配,返回true,否则返回false
{
if((ch1==')'&&ch2=='(')||(ch1==']'&&ch2=='[')||(ch1=='}'&&ch2=='{'))
return true;
return false;
}
void Match_Stack()//括号匹配的检验(自带 数据的输入 与 栈)
{
LinkStack s;
Init_Stack(s);
char ch;
cout<<"输入字符,输入@停止\n";
char e;
while(cin>>ch,ch!
2020-9-16//严蔚敏《数据结构》 //栈的应用举例:括号匹配的检验
最新推荐文章于 2022-03-19 15:07:03 发布