貌似用了野指针,找不到啊 调试不太擅长

higurasikagome 2013-11-29 01:38:05
假设称正读和反读都相同的字符序列为“回文”,例如,‘abba’和‘abcba’是回文,‘abcde’和‘ababab’则不是回文。试写一个算法判别读入的一个以‘@’为结束符的字符序列是否是“回文”。编程实现该程序。要求实现下列函数:
(1) 实现栈、队列的各个基本操作函数;
(2) Pal( )函数实现回文的判断;
(3) main( )函数进行调用。
[实现提示]
输入:需要进行判断的字符序列
输出:回文判断结果
注:队列必需用循环队列实现。

#include"stdio.h"
#include"stdlib.h"
#define INITSTACKSIZE 50
#define QUEUEMAXSIZE 50
#define INCREMENTSIZE 10
typedef struct
{
char *top;
char *base;
int stacksize;
}Stack;
typedef struct
{
char *qbase;
int front;
int rear;
}Queue;
void InitStack(Stack &J)
{
J.base=(char*)malloc(INITSTACKSIZE*sizeof(char));
if(!J.base)
exit(-1);
J.top=J.base;
J.stacksize=INITSTACKSIZE;
}
void DestoryStack(Stack &J)
{
free(J.base);
J.base=NULL;
J.top=NULL;
J.stacksize=0;
}
int StackEmpty(Stack J)
{
if(J.base==J.top)
return 1;
else
return 0;
}
void Push(Stack &J,char &e)
{
if(J.top>=J.base)
J.base=(char*)realloc(J.base,(INITSTACKSIZE+INCREMENTSIZE)*sizeof(char));
if(!J.base)
exit(-1);
J.top=J.base+J.stacksize;
J.stacksize+=INCREMENTSIZE;
*(J.top)++=e;
}
void Pop(Stack &J, char e)
{
if(StackEmpty(J))
exit(-1);
e=*--J.top;
}
void InitQueue(Queue &K)
{
K.qbase=(char*)malloc(QUEUEMAXSIZE*sizeof(char));
K.front=K.rear=0;
}
void DestoryQueue(Queue &K)
{
if(K.front==K.rear)
exit(-1);
free(K.qbase);
K.qbase=NULL;
K.front=K.rear=0;
}
int QueueEmpty(Queue K)
{
if(K.front==K.rear)
return 1;
else
return 0;
}
void EnQueue(Queue &K,char f)
{
if((K.rear+1)%QUEUEMAXSIZE==0)
exit(-1);
K.qbase[K.rear]=f;
K.rear=(K.rear+1)%QUEUEMAXSIZE;
}
void DeQueue(Queue &K,char f)
{
if(K.front==K.rear)
exit(-1);
f=K.qbase[K.front];
K.front=(K.front+1)%QUEUEMAXSIZE;
}

void Puen(Stack &J,Queue &K,char *r)
{
while((*r)!='@')
{
Push(J,*r);
EnQueue(K,*r);
r++;
}
}
void Pal(Stack &J,Queue &K)
{
while(*(J.top-1)==(K.qbase[K.front]))
{
Pop(J,*(J.top-1));
DeQueue(K,(K.qbase[K.front]));
}
if(StackEmpty(J)&& QueueEmpty(K))
printf("您输入的字符数列是回文数列!\n");
else
printf("您输入的字符数列不是回文数列!\n");
}

int main()
{
char a[50];
Stack s;
Queue q;
printf("请输入字符序列以‘@’加上回车结束:\n");
gets(a);
printf("您输入的字符数列为:\n");
puts(a);
InitStack(s);
InitQueue(q);
Puen(s,q,a);
Pal(s,q);
DestoryStack(s);
DestoryQueue(q);
return 0;
}
...全文
189 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
higurasikagome 2013-11-29
  • 打赏
  • 举报
回复
老师帮我检查了程序,解决了问题。
higurasikagome 2013-11-29
  • 打赏
  • 举报
回复
正确的代码
#include"stdio.h"
#include"stdlib.h"
#define INITSTACKSIZE 50
#define  QUEUEMAXSIZE 50
#define  INCREMENTSIZE 10
typedef struct 
{
	char *top;
	char *base;
	int stacksize;
}Stack;
typedef struct 
{
	char *qbase;
	int front;
	int rear;
}Queue;
void InitStack(Stack &J)
{
	J.base=(char*)malloc(INITSTACKSIZE*sizeof(char));
	if(!J.base)
		exit(-1);
	J.top=J.base;
	J.stacksize=INITSTACKSIZE;
}
void DestoryStack(Stack &J)
{
	free(J.base);
	J.base=NULL;
	J.top=NULL;
	J.stacksize=0;
}
int StackEmpty(Stack J)
{
	if(J.base==J.top)
		return 1;
	else 
		return 0;
}
void Push(Stack &J,char e)
{
	if((J.top-J.base)>=J.stacksize){
		J.base=(char*)realloc(J.base,(INITSTACKSIZE+INCREMENTSIZE)*sizeof(char));
	if(!J.base)
		exit(-1);
	J.top=J.base+J.stacksize;
	J.stacksize+=INCREMENTSIZE;
	}
	*(J.top)++=e;
}
void Pop(Stack &J, char &e)
{
	if(StackEmpty(J))
		exit(-1);
	e=*--J.top;
}
void InitQueue(Queue &K)
{
	K.qbase=(char*)malloc(QUEUEMAXSIZE*sizeof(char));
	K.front=K.rear=0;
}
void DestoryQueue(Queue &K)
{
	if(K.front==K.rear)
		exit(-1);
	free(K.qbase);
	K.qbase=NULL;
	K.front=K.rear=0;
}
int QueueEmpty(Queue K)
{
	if(K.front==K.rear)
		return 1;
	else 
		return 0;
}
void EnQueue(Queue &K,char f)
{
	if((K.rear+1)%QUEUEMAXSIZE==0)
		exit(-1);
	K.qbase[K.rear]=f;
	K.rear=(K.rear+1)%QUEUEMAXSIZE;
}
void DeQueue(Queue &K,char &f)
{
	if(K.front==K.rear)
		exit(-1);
	f=K.qbase[K.front];
	K.front=(K.front+1)%QUEUEMAXSIZE;
}

void Puen(Stack &J,Queue &K,char *r)
{
	while((*r)!='@')
	{
		Push(J,*r);
		EnQueue(K,*r);
		r++;
	}
}
void Pal(Stack &J,Queue &K)
{char a,b;
//	while(*(J.top-1)==(K.qbase[K.front]))
	/*printf("\nStack: ");
	char *p=J.top-1;
	while (p!=J.base)
	{	printf("%d ",*p);
		p--;
	}
	printf("%d ",*p);
	*/

	while(!StackEmpty(J))
	{
		Pop(J,a);
		DeQueue(K,b);
		printf("a=%d,b=%d\n",a,b);
		if (a!=b)
			break;
		

	}
	if(StackEmpty(J)==1)
		printf("您输入的字符数列是回文数列!\n");
	else
		printf("您输入的字符数列不是回文数列!\n");
}

int main()
{
	char a[50];
	Stack s;
	Queue q;
	printf("请输入字符序列以‘@’加上回车结束:\n");
	gets(a);
	printf("您输入的字符数列为:\n");
	puts(a);
	InitStack(s);
	InitQueue(q);
	Puen(s,q,a);
	Pal(s,q);
	DestoryStack(s);
	DestoryQueue(q);
	return 0;
}
N_Sev7 2013-11-29
  • 打赏
  • 举报
回复
呐,编程呢,最重要的就是调试,如果你不会调试 那就学习如何调试。 呐,调试呢,最重要的就是跟踪,如果你不会跟踪 那就学习如何按F9 F10 F11
lee_鹿游原 2013-11-29
  • 打赏
  • 举报
回复

void Push(Stack &J,char &e) //这里出现错误,自己调试跟踪吧
ouyh12345 2013-11-29
  • 打赏
  • 举报
回复
调试啊,看调用堆栈
higurasikagome 2013-11-29
  • 打赏
  • 举报
回复
#include"stdio.h"
#include"stdlib.h"
#define INITSTACKSIZE 50
#define  QUEUEMAXSIZE 50
#define  INCREMENTSIZE 10
typedef struct 
{
	char *top;
	char *base;
	int stacksize;
}Stack;
typedef struct 
{
	char *qbase;
	int front;
	int rear;
}Queue;
void InitStack(Stack &J)
{
	J.base=(char*)malloc(INITSTACKSIZE*sizeof(char));
	if(!J.base)
		exit(-1);
	J.top=J.base;
	J.stacksize=INITSTACKSIZE;
}
void DestoryStack(Stack &J)
{
	free(J.base);
	J.base=NULL;
	J.top=NULL;
	J.stacksize=0;
}
int StackEmpty(Stack J)
{
	if(J.base==J.top)
		return 1;
	else 
		return 0;
}
void Push(Stack &J,char e)
{
	if((J.top-J.base)>=J.stacksize){
		J.base=(char*)realloc(J.base,(INITSTACKSIZE+INCREMENTSIZE)*sizeof(char));
		if(!J.base)
			exit(-1);
		J.top=J.base+J.stacksize;
		J.stacksize+=INCREMENTSIZE;
	}
	*(J.top)++=e;
}
void Pop(Stack &J)
{
	if(StackEmpty(J))
		exit(-1);
	--J.top;
}
void InitQueue(Queue &K)
{
	K.qbase=(char*)malloc(QUEUEMAXSIZE*sizeof(char));
	K.front=K.rear=0;
}
void DestoryQueue(Queue &K)
{
	if(K.front==K.rear)
		exit(-1);
	free(K.qbase);
	K.qbase=NULL;
	K.front=K.rear=0;
}
int QueueEmpty(Queue K)
{
	if(K.front==K.rear)
		return 1;
	else 
		return 0;
}
void EnQueue(Queue &K,char f)
{
	if((K.rear+1)%QUEUEMAXSIZE==0)
		exit(-1);
	K.qbase[K.rear]=f;
	K.rear=(K.rear+1)%QUEUEMAXSIZE;
}
void DeQueue(Queue &K)
{
	if(K.front==K.rear)
		exit(-1);
	K.front=(K.front+1)%QUEUEMAXSIZE;
}

void Puen(Stack &J,Queue &K,char *r)
{
	while((*r)!='@')
	{
		Push(J,*r);
		EnQueue(K,*r);
		r++;
	}
}
void Pal(Stack &J,Queue &K)
{
	//char a,b;
	/*while(*(J.top-1)==(K.qbase[K.front]))
	printf("\nStack: ");
	char *p=J.top-1;
	while (p!=J.base)
	{	printf("%d ",*p);
	p--;
	}
	printf("%d ",*p);
	*/
	
	//while(!StackEmpty(J))
	while(*(J.top-1)==(K.qbase[K.front]))
	{
		Pop(J);//Pop(J,a)
		DeQueue(K);//DeQueue(K),
		//printf("a=%d,b=%d\n",a,b);
		//if (a!=b)
		//break;
		
		
	}
	if(StackEmpty(J))
		printf("您输入的字符数列是回文数列!\n");
	else
		printf("您输入的字符数列不是回文数列!\n");
}

int main()
{
	char a[50];
	Stack s;
	Queue q;
	printf("请输入字符序列以‘@’加上回车结束:\n");
	gets(a);
	printf("您输入的字符数列为:\n");
	puts(a);
	InitStack(s);
	InitQueue(q);
	Puen(s,q,a);
	Pal(s,q);
	DestoryStack(s);
	DestoryQueue(q);
	return 0;
}
  • 打赏
  • 举报
回复
引用 1 楼 rogone 的回复:

for(int i = 0; i < n / 2; ++i)
{
    if(s[i] != s[n - 1 - i])
    {
        return false;
    }
}
return true
直接比较就行了,用栈是个什么情况
没看最后一个条件,我错了,请无视
  • 打赏
  • 举报
回复

for(int i = 0; i < n / 2; ++i)
{
    if(s[i] != s[n - 1 - i])
    {
        return false;
    }
}
return true
直接比较就行了,用栈是个什么情况

69,378

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧