在做二叉排序树的应用时出错。。

astraydog 2009-12-09 06:25:00
我写的程序是在VC下写的,在输完45 ,12 ,53后就弹出0x00401174指令引用的0xcdcdcdcd内存,该内存不
能为read要终止程序,请单击确定
要调试程序,请单击取消,我实在找不出错误所在,请大家帮忙看看哪里出错啦。。。
#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>
#define LEN sizeof(struct btnode)
typedef struct btnode
{ int data;
struct btnode *lchild,*rchild;
}Bitnode,*bitree;

bitree create();
void insert(bitree *p,bitree s);
void inorder(bitree t);

void main()
{bitree h;
printf("input binary sort tree :");
h=create();
inorder(h);
}

bitree create()
{bitree t,h;
int k;
t=NULL;
scanf("%d",&k);
while(k)//<=0结束
{h=(bitree)malloc(LEN);
h->data=k;
h->lchild=NULL;h->rchild;
insert(&t,h);
scanf("%d",&k);
}
return t;
}

void insert(bitree *p,bitree s)
{
if(*p==NULL) *p=s;
else
if(s->data<(*p)->data)
insert(&((*p)->lchild),s);
else if(s->data>(*p)->data)
insert(&((*p)->rchild),s);

}

void inorder(bitree t)
{
if(t)
{inorder(t->lchild);
printf("%d",t->data);
inorder(t->rchild);
}
}
...全文
109 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
astraydog 2009-12-12
  • 打赏
  • 举报
回复
谢谢啊。。知道啦。。
yeke6253 2009-12-11
  • 打赏
  • 举报
回复
厉害~~~!!!!!
mmilmf 2009-12-11
  • 打赏
  • 举报
回复
加return的意思是在递归的过程中,函数要有个出口。不然程序不能结束,会出错!
astraydog 2009-12-11
  • 打赏
  • 举报
回复
初始化部分我已经知道了,inorder为什么要加个return 的?
deng1243 2009-12-09
  • 打赏
  • 举报
回复
void inorder(bitree t) 
{
if(t)
{
inorder(t->lchild);
printf("%d ",t->data);
inorder(t->rchild);
}
else
return;
}
inorder最好这样,加个递归出口
deng1243 2009-12-09
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define LEN sizeof(struct btnode)
typedef struct btnode
{
int data;
struct btnode *lchild,*rchild;
}Bitnode,*bitree;

bitree create();
void insert(bitree *p,bitree s);
void inorder(bitree t);

void main()
{
bitree h;
printf("input binary sort tree :");
h=create();
inorder(h);
}

bitree create()
{
bitree t,h;
int k;
t=NULL;
scanf("%d",&k);
while(k)// <=0结束
{
h=(bitree)malloc(LEN);
h->data=k;
h->lchild=NULL;
h->rchild=NULL; //这里,都要初始化
insert(&t,h);
scanf("%d",&k);
}
return t;
}

void insert(bitree *p,bitree s)
{
if(*p==NULL)
*p=s;
else if(s->data <(*p)->data)
insert(&((*p)->lchild),s);
else if(s->data>(*p)->data)
insert(&((*p)->rchild),s);
}

void inorder(bitree t)
{
if(t)
{
inorder(t->lchild);
printf("%d",t->data);
inorder(t->rchild);
}
}
注意create函数

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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