2020-9-16//严蔚敏《数据结构》 //栈的应用举例:括号匹配的检验

//严蔚敏《数据结构》
//栈的应用举例:括号匹配的检验
//自学数据结构中,加油啊!!!
#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!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值