C语言中的数据结构问题

编写程序判断读入的一个以@为结束符的字符序列是否为回文。
思路是将字符序列分别输入一个栈和队列,然后分别执行出栈和出队来进行比较

代码可以编译,但就是运行不了

#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);
}

编译没问题,就是达不到我要的效果,不知道怎么回事

/*
你好, 程序思路是:
先把输入的东西一起入栈和入队列.
如Q, a, b, c, b, a
S, a, b, c ,b, a
等用户输入@后, Q出队列, S出栈,
判断两个出栈的内容是否一致, 是否同是为空, 如果不是, 说明不是回文.
// 此程序运行正确.
*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define MAXSIZE 100

/*#include "debug_zhu.h"*/
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));
bzero(S, 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 -1; // 失败为 -1 // zhuxh 出栈和出队列的返回值要一致.
} else {
*x=S->data[S->top];
S->top--;
return 0; // 成功为 0 // zhuxh
}
}

PSeqQueue Init_SeqQueue()
{
PSeqQueue Q;
Q=(PSeqQueue)malloc(sizeof(SeqQueue));
bzero(Q, 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 0;
}
}

int compare(char ch)
{
char ch_Statck;
char ch_Queue;
PSeqStack S;
S = Init_SeqStack();
Push_SeqStack(S,ch);

PSeqQueue Q;
Q = Init_SeqQueue();
In_SeqQueue(Q, ch);

bool bIn = true; // 入栈.
while( (ch = getchar())!='@') {
Push_SeqStack(S, ch);
In_SeqQueue(Q, ch);
}
printf(" Queue = %s \n", Q->data + 1 );
printf(" stack = %s \n", S->data );
int ret = -1;
do{
// 请确保 Out_SeqQueue 和 Pop_SeqStack 返回值一致.
if( Out_SeqQueue(Q, &ch_Queue ) == 0 && Pop_SeqStack(S, &ch_Statck ) == 0 ){
if( ch_Queue != ch_Statck ){
/*printf("111!!!!!!!! \n");*/
break;
}
}else{
/*printf("222 !!!!!!!! \n");*/
break;
}
/*printf(" ch_Queue = %c , ch_Statck = %c", ch_Queue, ch_Statck );*/
}while (1);

if( (ch_Queue == ch_Statck) && (Empty_SeqQueue(Q) && Empty_SeqStack(S))){
printf("\n 匹配\n");
}else {
// 未完成比较
printf("\n 不匹配\n");
}

// 要记得内存释放.
free( Q );
Q = NULL;
free(S);
S = NULL;
}

int main(int argc, char ** argv )
{
char a;
printf("请输入字符串");
scanf("%c",&a);
compare(a);
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-03-22
程序基本上是不错。
你的思路也是对的。

只是算法上有点不清楚!
应该是先把字符,入队和入栈;然后再把出栈的字符 与 出队的字符进行比较,如果完全一致,就说明是匹配的。

只需要把函数compare 调整一下即可。
调整如下,请看注释。

#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;
PSeqQueue Q;
char ch2;

S=Init_SeqStack(); //初始化
Q=Init_SeqQueue();//初始化

do
{
Push_SeqStack(S,ch); //入栈
In_SeqQueue(Q,ch);//入队

}while((ch=getchar())!='@');

while(1) //比较出队和出栈
{
if(Empty_SeqStack(S)==1) //栈空,则退出循环
break;
Pop_SeqStack(S,&ch1); //栈不为空,出栈操作,出栈到ch1;
if(Empty_SeqQueue(Q)==1) //队空,则退出循环
break;
Out_SeqQueue(Q,&ch2); //队不为空,出队操作,则出队到ch2;

if(ch1 != ch2) //比较,如果不相等,则退出
break;
}

if(ch1==ch2)//比较最后一次的结果
{
printf("匹配");
}
else
{
printf("不匹配");
}
return 0;
}

void main()
{
char a;
printf("请输入字符串\n");
scanf("%c",&a);
compare(a);
}
第2个回答  2012-03-20
//-------------------------------
//1、
void main()
{
// char?字符串?你确信?
char a;
printf("请输入字符串");
scanf("%c",&a);
compare(a);
}

//-------------------------------
// 2、
int compare(char ch)
{
PSeqStack S;
char ch1;
S=Init_SeqStack();
Push_SeqStack(S,ch);
// ch=getchar()? 难道不是ch1=getchar()?下面ch2哪里也是一样
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("不匹配");
}
}
第3个回答  2012-03-24
这么麻烦先用ltoa转存字符型,再用strcpy复制一份,再用strrev倒叙,在用strcmp比较下是否为0,如果是0,那就是回文了
第4个回答  2012-03-21
首先在main里应该是定义char* a, scanf("%s",a)
还有那么麻烦干嘛,可以直接用指针分别指向字符串头和尾,比较久可以了
相似回答