#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef char SElemType;
typedef char QElemType;
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;//定义栈 (链栈)
typedef struct QNode
{
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
QueuePtr front;
QueuePtr rear;
}LinkQueue;//定义队列
int InitStack(SqStack&S)
{
S.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S.base)
exit(-1);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
}//初始化栈
int Push(SqStack&S,char&e)
{
if(S.top-S.base==S.stacksize)
{
S.base=(SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S.base) exit(-1);
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
}//入栈操作
int Pop(SqStack&S,char&a1)
{
if(S.top==S.base)
return 0;
a1=*--S.top;
}//出栈操作
int InitQueue(LinkQueue&Q)
{
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q.front)
{
printf("创建失败");
exit(-1);
}
Q.front->next=NULL;
return 1;
}//初始化队列
int EnQueue(LinkQueue &Q,char&e)
{
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
if(!p)
exit(-1);
p->data =e;
p->next =NULL;
if(Q.front->next==NULL)
Q.front->next=p;
Q.rear->next=p;
Q.rear=p;
return 1;
}//入队操作
int DeQueue(LinkQueue&Q,char&a2)
{
QueuePtr p;
if(Q.front==Q.rear )
{
printf("空队列,无法进行操作 \n");
return -1;
}
p=Q.front->next;
a2=p->data;
Q.front->next=p->next;
if(Q.rear==p)
{
Q.rear=Q.front;
free(p);
}
return 1;
}//出队操作
int main()
{
SqStack S;
InitStack(S);
LinkQueue Q;
InitQueue(Q);
char e;
char a1,a2;
printf("输入字符序列(以@结尾): \n");
e=getchar();
while(e!='@')
{
Push(S,e);
EnQueue(Q,e);
e=getchar();
}
while(Q.front!=Q.rear)
{
Pop(S,a1);
DeQueue(Q,a2);
if(a1!=a2)
{
printf("输入的字符串不是回文!\n");
break;
}
}
if(Q.front==Q.rear)
printf("输入的字符串是回文!\n");
return 0;
}//主函数
运行效果