编写程序判断读入的一个以@为结束符的字符序列是否为回文。
思路是将字符序列分别输入一个栈和队列,然后分别执行出栈和出队来进行比较
代码可以编译,但就是运行不了
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define MAXSIZE 100
typedef char DataType;
typedef struct{
DataType data[MAXSIZE];
int top;
}SeqStack,*PSeqStack;
typedef struct{
DataType data[MAXSIZE];
int front,rear;
}SeqQueue,*PSeqQueue;
PSeqStack Init_SeqStack(void)
{
PSeqStack S;
S=(PSeqStack)malloc(sizeof(SeqStack));
if(S)
S->top=-1;
return S;
}
char Push_SeqStack(PSeqStack S,DataType x)
{
if (S->top==MAXSIZE-1)
return 0;
else
{
S->top++;
S->data[S->top]=x;
return 1;
}
}
char Empty_SeqStack(PSeqStack S)
{
if(S->top==-1)
return 1;
else
return 0;
}
char Pop_SeqStack(PSeqStack S,DataType *x)
{
if(Empty_SeqStack(S))
return 0;
else
{
*x=S->data[S->top];
S->top--;
return -1;
}
}
PSeqQueue Init_SeqQueue()
{
PSeqQueue Q;
Q=(PSeqQueue)malloc(sizeof(SeqQueue));
if(Q)
{
Q->front=0;
Q->rear=0;
}
return Q;
}
char Empty_SeqQueue(PSeqQueue Q)
{
if(Q && Q->front==Q->rear)
return (1);
else
return (0);
}
char In_SeqQueue(PSeqQueue Q,DataType x)
{
if((Q->rear+1)%MAXSIZE==Q->front)
{
printf("队满");
return -1;
}
else
{
Q->rear=(Q->rear+1)%MAXSIZE;
Q->data[Q->rear]=x;
return 1;
}
}
char Out_SeqQueue(PSeqQueue Q,DataType *x)
{
if(Empty_SeqQueue(Q))
{
printf("队空");
return -1;
}
else
{
Q->front=(Q->front+1)%MAXSIZE;
*x=Q->data[Q->front];
return 1;
}
}
int compare(char ch)
{
PSeqStack S;
char ch1;
S=Init_SeqStack();
Push_SeqStack(S,ch);
while((ch=getchar())!='@')
{
if(Empty_SeqStack(S))
return 0;
Pop_SeqStack(S,&ch1);
}
PSeqQueue Q;
char ch2;
Q=Init_SeqQueue();
In_SeqQueue(Q,ch);
while((ch=getchar())!='@')
{
if(Empty_SeqQueue(Q))
return 0;
Out_SeqQueue(Q,&ch2);
}
strcmp(&ch1, &ch2);
if(ch1==ch2)
{
printf("匹配");
}
else
{
printf("不匹配");
}
}
void main()
{
char a;
printf("请输入字符串");
scanf("%c",&a);
compare(a);
}
编译没问题,就是达不到我要的效果,不知道怎么回事