#include <stdio.h>
#define m 100
/* 定义栈*/
typedef struct {
char stack[m];
int top;
}stackstru;
/*初始化栈*/
int stinit(stackstru *s)
{
s->top=0;
return 1;
}
/*判断栈是否空*/
int stempty(stackstru *s)
{
if(s->top==0)
{
return 0;
}
else
{
return 1;
}
}
/*入栈操作*/
int stpush(stackstru *s,char x)
{
if(s->top==m)
{
printf("The stack is overflow!\n");
return 0;
}
else
{
s->top=s->top+1;
s->stack[s->top]=x;
return 1;
}
}
/*出栈操作*/
char stpop(stackstru *s)
{
char y;
if(s->top==0)
{
printf("The stack is empty!\n");
return '';
}
else
{
y=s->stack[s->top];
s->top=s->top-1;
return y;
}
}
/*定义队列*/
typedef struct {
char queue[m];
int front;
int rear;
}queuestru;
/*初始化队列*/
int quinit(queuestru *q)
{
q->front=0;
q->rear=0;
return 1;
}
/*判断队列是否为空*/
int quempty(queuestru *q)
{
if(q->front==q->rear)
{
return 0;
}
else
{
return 1;
}
}
/*入队操作*/
int enqueue(queuestru *q,char e)
{
if((q->rear+1)%m==q->front)
{
printf("The queue is overflow!\n");
return 0;
}
else
{
q->queue[q->rear]=e;
q->rear=(q->rear+1)%m;
return 1;
}
}
/*出队操作*/
char dequeue(queuestru *q)
{
char f;
if(q->front==q->rear)
{
printf("The queue is empty!\n");
return 0;
}
else
{
f=q->queue[q->front];
q->front=(q->front+1)%m;
return f;
}
}
/*主程序*/
main()
{
char c;
int v;
stackstru *s=(stackstru *)malloc(sizeof(stackstru));
stackstru *q=(stackstru *)malloc(sizeof(queuestru));
stinit(s);
quinit(q);
printf("Input a string:\n");//输入字符串,输入@标示输入结束。
while((c=getchar())!='@')
{
putchar(c);
stpush(s,c);
enqueue(q,c);
}
printf("End input!\n");
while(stempty(s))
{
if(stpop(s)==dequeue(q))
{
v=1;
continue;
}
else
{
v=0;
break;
}
}
if(v==1)
{
printf("This string is palindrome!\n");
}
else
{
printf("This string isn't palindrome!\n");
}
}