链式栈回文字符串的判断(C++版)

大家好我是大一新生,如果代码有啥错误和改进的地方可以评论哦,谢谢观念看;

#include<iostream>
#include<iomanip>
using namespace std;
#define ok 1
#define error 0
#define Selemtype char
#define Status int
#define MAXSIZE 100

typedef struct stack{//链式栈的结构 
    Selemtype data;
    struct stack *next;
}SqStack,*LinkStack;

Status InitStack(LinkStack &S)//初始化 
{
    S=NULL;
    return ok;
}

Status Push(LinkStack &S,Selemtype e)//入栈 
{
    LinkStack p;
    p=new SqStack;//申请一个新节点后将元素压入 
    p->data=e;
    p->next=S;
    S=p;
    return ok;
}

Status Pop(LinkStack &S,Selemtype &e)//出栈 
{
    if(S==NULL)//判断是否为空栈 
    {
        cout<<"出栈失败"<<endl;
        return error;
    }
    LinkStack p;
    p=S;
    e=p->data;
    S=S->next;
    delete p;
    return ok;
}

Status huiwen(LinkStack &S)//判断回文函数 
{
    int i=0,n=0;
    Selemtype e,str[MAXSIZE];//用字符数组存储字符串 
    while(cin>>str[n])//输入元素并将元素压入栈中 
    {
        if(str[n]=='#')
        break;
        Push(S,str[n]);
        n++;
    }
    if(str[i]=='#')//排除空栈的可能 
    return error;
    while(i<n)//判断回文 
    {
        Pop(S,e);
        if(str[i]!=e)
        return error;
        i++;
    }
    return ok;
}

main()
{
    LinkStack S;
    InitStack(S);
    if(!huiwen(S))
    cout<<"Yes"<<endl;
    else
    cout<<"No"<<endl;
    
}

  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值