#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MaxStackSize 50
typedef char ElemType;
typedef struct snode
{
ElemType data;
struct snode *next;
}SingleLinkedNode;
void StackInitiate(SingleLinkedNode **head) //初始化
{
if((*head=(SingleLinkedNode *)malloc(sizeof(SingleLinkedNode)))==NULL)
exit(1);
(*head)->next=NULL;
}
int StackNotEmpty(SingleLinkedNode *head) //是否为空
{
if(head->next==NULL)
return 0;
else
return 1;
}
int StackPush(SingleLinkedNode *head,ElemType x) //入栈
{
SingleLinkedNode *p;
if((p=(SingleLinkedNode *)malloc(sizeof(SingleLinkedNode)))==NULL)
{
printf("内存空间不足无法插入!\n");
return 0;
}
p->data=x;
p->next=head->next;
head->next=p;
}
int StackPop(SingleLinkedNode *head,ElemType *d) //删除元素(弹出)
{
SingleLinkedNode *p=head->next;
if(p=NULL)
{
printf("堆栈已空出错!");
return 0;
}
head->next=p->next;
*d=p->data;
free(p);
return 1;
}
typedef struct qnode //队列
{
ElemType data;
struct qnode *next;
}LinkedQueueNode;
typedef struct
{
LinkedQueueNode *front;
LinkedQueueNode *rear;
}LQueue;
void QueueInitiate(LQueue *Q) //初始化
{
Q->rear=NULL;
Q->front=NULL;
}
int QueueNotEmpty(LQueue Q) //是否为空
{
if(Q.front==NULL)
return 0;
else
return 1;
}
int QueueAppend(LQueue *Q,ElemType x) //入队列
{
LinkedQueueNode *p;
if((p=(LinkedQueueNode *)malloc(sizeof(LinkedQueueNode)))==NULL)
{
printf("内存空间不足!");
return 0;
}
p->data=x;
p->next=NULL;
if(Q->rear!=NULL)
Q->rear->next=p;
Q->rear=p;
if(Q->front==NULL)
Q->front=p;
return 1;
}
int QueueDelete(LQueue *Q,ElemType *d) //出队列(删除)
{
LinkedQueueNode *p;
if(Q->front==NULL)
{
printf("队列已空无数据出队列!\n");
return 0;
}
else
{
*d=Q->front->data;
p=Q->front;
Q->front=Q->front->next;
if(Q->front==NULL)
Q->rear=NULL;
free(p);
return 1;
}
}
typedef struct //顺序栈(进制 )
{
ElemType stack[MaxStackSize];
int top;
}SequenceStack;
void StackInitiate(SequenceStack *S)
{
S->top=0;
}
int StackNotEmpty(SequenceStack *S)
{
if(S->top<=0)
return 0;
else
return 1;
}
int StackPush(SequenceStack *S,ElemType x)
{
if(S->top>=MaxStackSize)
{
printf("堆栈已满无法插入!\n");
return 0;
}
else
{
S->stack[S->top]=x;
S->top++;
return 1;
}
}
int StackPop(SequenceStack *S,ElemType *d)
{
if(S->top<=0)
{
printf("堆栈已空!\n");
return 0;
}
else
{
S->top--;
*d=S->stack[S->top];
return 1;
}
}
void main() //主函数在这里
{
SingleLinkedNode *head;
LQueue Q;
SequenceStack *myStack;
int i, decimal,k,g;
char str[20],x,d;
StackInitiate(&head);
StackInitiate(myStack);
QueueInitiate(&Q);
printf("输入字符串:");
gets(str);
i=0;
while(i<strlen(str))
{
StackPush(head,str[i]);
QueueAppend(&Q,str[i]);
i++;
}
g=0;
while(StackNotEmpty(head)&&QueueNotEmpty(&Q)) //提示说这里错了!!!!!!
{
StackPop(head,&x);
QueueDelete(&Q,&d);
if(x!=d)
break;
g++;
}
if(g<strlen(str))
printf("不是回文!\n");
if(g==strlen(str))
printf("是回文!\n");
}