在做二叉排序树的应用时出错。。
我写的程序是在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);
}
}
[解决办法]
- C/C++ code
#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); } }