大家好我是大一新生,如果代码有啥错误和改进的地方可以评论哦,谢谢观念看;
#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;
}