栈的应用-括号的匹配

一种括号的情况
#include <iostream></span>
#include <stdlib.h>
#include <stdbool.h>
using namespace std;
typedef struct node
{
    char AL;
    struct node* next;
}qnode,*pnode;
typedef struct stack
{
    pnode top;
    pnode botton;
}qstack,*pstack;
void creat_(pstack s)
{
    s->top=(pnode)malloc(sizeof(node));
    if(s->top==NULL)
    {
        cout<<"fault";
        return;
    }
    s->botton=s->top;
    s->botton->next=NULL;
}
void push(pstack s,char e)
{
    pnode new_=(pnode)malloc(sizeof(qnode));
    new_->AL=e;
    new_->next=s->top;
    s->top=new_;
}
bool empty(pstack s)
{
   if(s->top==s->botton)
   {
       return true;
   }
   else
   {
       return false;
   }
}
void pop(pstack s)
{
   if(empty(s))
   {
       cout<<"出栈失败";
   }
   else
   {
       pnode r;
       r=s->top;
       s->top=r->next;
       free(r);
   }
}
int lengh(pstack s)
{
    int i=0;
    pnode q=s->top;
    while(q!=s->botton)
    {
        q=q->next;
        i++;
    }
    return i;
}
int check()
{
    pstack s;
    creat_(s);
    char ch;
    ch=getchar();
    while(ch!='\n')//当等于换行的时候跳出循环
    {
        if(ch=='(')//等于(入栈
        {
            push(s,ch);
        }
        else if(ch==')')//等于)如果为空则缺少(返回负一
        {
            if(empty(s))
            {
                return -1;
            }
            else
            {
                pop(s);//否则的话栈顶出栈
            }
        }
        ch=getchar();//再次输入
    }
    return lengh(s);//返回最后栈的长度
}
int main()
{
    int result;
    result=check();
    if(result==0)
    {
        cout<<"right";
    }
    else if(result>0)
    {
        cout<<"min";
    }
    else if(result<0)
    {
        cout<<"min(";
    }
    return 0;
}
------------------------
两种括号的情况
#include <iostream>
#include <stdlib.h>
#include <stdbool.h>
using namespace std;
typedef struct node
{
    char AL;
    struct node* next;
}qnode,*pnode;
typedef struct stack
{
    pnode top;
    pnode botton;
}qstack,*pstack;
void creat_(pstack s)
{
    s->top=(pnode)malloc(sizeof(node));
    if(s->top==NULL)
    {
        cout<<"fault";
        return;
    }
    s->botton=s->top;
    s->botton->next=NULL;
}
void push(pstack s,char e)
{
    pnode new_=(pnode)malloc(sizeof(qnode));
    new_->AL=e;
    new_->next=s->top;
    s->top=new_;
}
bool empty(pstack s)
{
   if(s->top==s->botton)
   {
       return true;
   }
   else
   {
       return false;
   }
}
void pop(pstack s)
{
   if(empty(s))
   {
       cout<<"出栈失败";
   }
   else
   {
       pnode r;
       r=s->top;
       s->top=r->next;
       free(r);
   }
}
int lengh(pstack s)
{
    int i=0;
    pnode q=s->top;
    while(q!=s->botton)
    {
        q=q->next;
        i++;
    }
    return i;
}
int check()
{
    char l;
    pstack s;
    creat_(s);
    char ch;
    ch=getchar();//getchar略过空格
    while(ch!='\n')//当等于换行的时候跳出循环
    {
        if(ch=='(')//等于(入栈
        {
            push(s,ch);
        }
        else if(ch==')')//等于)如果为空则缺少(返回负一
        {
            if(empty(s))
            {
                return -1;
            }
            else
            {   if(s->top->AL=='(')
                pop(s);//否则的话栈顶出栈
            }
        }
        else if(ch=='{')
        {
            push(s,ch);
        }
        else if(ch=='}')
        {
    
            if(empty(s))
            {
                return -1;
            }
            else
            {
                if(s->top->AL=='{')
                {
                    pop(s);
                }
            }
        }
        ch=getchar();//再次输入
    }
    return lengh(s);//返回最后栈的长度
}
int main()
{
    int result;
    result=check();
    if(result==0)
    {
        cout<<"right";
    }
    else if(result>0)
    {
        cout<<"min";
    }
    else if(result<0)
    {
        cout<<"short ( or {";
    }
    return 0;
}



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值