#include <iostream>
#include <stdlib.h>
#include <stdbool.h>
using namespace std;
typedef struct node
{
char c;
struct node* pnext;
}qnode,*pnode;
typedef struct stack
{
pnode top;
pnode botton;
}qstack,*pstack;
void init(pstack s)
{
s->top=(pnode)malloc(sizeof(qnode));
s->botton=s->top;
}
bool empty(pstack s)
{
if(s->top==s->botton)
return true;
else
return false;
}
void push(pstack s,char c_)
{
pnode q;
q=(pnode)malloc(sizeof(qnode));
q->c=c_;
q->pnext=s->top;
s->top=q;
}
char gettop(pstack s)
{
return s->top->c;
}
void pop(pstack s)
{
pnode q;
if(empty(s))
cout<<"出栈失败";
else
{
q=s->top;
s->top=q->pnext;
free(q);
}
}
int lengh(pstack s)
{
int i;
pnode q=s->top;
if(q==s->botton)
i=0;
return i;
}
void judge(char a[],int i,int &lengh_)
{
stack s;
init(&s);
int j;
char k;
for(j=0;j<(i/2);j++)
{
push(&s,a[j]);
}
if(i%2==0)//如果是双数,则从i%2开始于栈顶比较
j=i/2;
else
j=i/2+1;//如果是单数,则从i%2+1开始比较
for(;j<i;j++)
{
k=gettop(&s);
if(a[j]==k)
{
pop(&s);
}
}
lengh_=lengh(&s);
}
int main()
{
int i=0,l;
char ch;
char a[100];
while((ch=getchar())!='\n')
{
a[i]=ch;
i++;
}
judge(a,i,l);
if(l==0)
{cout<<"yes";
}
else
{
cout<<"no";
}
}
98-6(栈-回文的使用)
最新推荐文章于 2024-04-25 08:31:40 发布