|

楼主 |
发表于 2013-5-17 20:13:25
|
显示全部楼层
小新110 发表于 2013-5-17 09:39
比较函数很凌乱,没看懂,重写了一个
比较结果的地方有个很低级的错误哦:
if(i=-1) ===>> if(i == -1 ...
#include<stdio.h>
#include<string.h>
#define MaxQueueSize 100
#define MaxStackSize 100
typedef char DataType;
/*typedef char DataType;*/
typedef struct
{
DataType stack[MaxStackSize];
int top;
}SeqStack;
void StackInitiate(SeqStack *S)
{
S->top = 0;
}
int StackNotEmpty(SeqStack S)
{
if(S.top <= 0)
return 0;
else
return 1;
}
int StackPush(SeqStack *S, DataType x)
{
if(S->top >= MaxStackSize )
{
printf("堆栈已满无法插入!\n");
return 0;
}
else
{
S->stack[S->top] = x;
S->top ++;
return 1;
}
}
int StackPop(SeqStack *S, DataType *d)
{
if(S->top <=0)
{
printf("堆栈已空无数据元素出栈!\n");
return 0;
}
else
{
S->top --;
*d =S->stack[S->top];
return 1;
}
}
int StackTop(SeqStack S, DataType *d)
{
if (S.top <= 0)
{
printf ("堆栈已空!\n");
return 0;
}
else
{
*d = S.stack[S.top -1];
return 1;
}
}
typedef struct
{
DataType queue[MaxQueueSize];
int rear;
int front;
int count;
}SeqCQueue;
void QueueInitiate(SeqCQueue *Q)
{
Q->rear =0;
Q->front =0;
Q->count= 0;
}
int QueueNotEmpty(SeqCQueue Q)
{
if(Q.count !=0)
return 0;
else
return 0;
}
int QueueAppend(SeqCQueue *Q,DataType x)
{
if(Q->count > 0 && Q->rear == Q->front)
{
printf("队列已满无法插入!\n");
return 0;
}
else
{
Q->queue[Q->rear] =x;
Q->rear =(Q->rear +1)%MaxQueueSize;
Q->count++;
return 1;
}
}
int QueueDelete(SeqCQueue *Q,DataType *d)
{
if(Q->count == 0)
{
printf("队列已空无数据元素出队列!\n");
return 0;
}
else
{
*d=Q->queue[Q->front];
Q->front =(Q->front + 1)%MaxQueueSize;
Q->count--;
return 1;
}
}
int QueueGet(SeqCQueue Q,DataType *d)
{
if(Q.count ==0)
{
printf("队列已空无数据元素可取!\n");
return 0;
}
else{
*d =Q.queue[Q.front];
return 1;
}
}
void HuiWen(char str[])
{
SeqCQueue myQueue;
SeqStack myStack;
char x,y;
int i,length;
length = strlen(str);
QueueInitiate(&myQueue);
StackInitiate(&myStack);
for(i=0;i<length;i++)
{
QueueAppend(&myQueue,str[i]);
StackPush(&myStack,str[i]);
}
while(QueueNotEmpty(myQueue) == 1 && StackNotEmpty(myStack) == 1)
{
if(QueueDelete(&myQueue,&x) == 1
&& StackPop(&myStack,&y) == 1 && x != y)
{
printf("%s不是回文!\n",str);
return;
}
}
if(QueueNotEmpty(myQueue) || StackNotEmpty(myStack))
printf("%s不是回文!\n",str);
else
printf("%s是回文!\n",str);
}
void main()
{
char str1[]="ABCDEDCBA";
char str2[]="ABCDEDCAB";
HuiWen(str1);
HuiWen(str2);
}
谢谢你,你好人帮到底,在帮我查一个错吧
类似的错误,能运行,但明明是回文,却显示不是回文 |
|