高手看看这个C程序

来源:百度知道 编辑:UC知道 时间:2024/06/07 19:37:15
小弟新手,这个二叉树程序不能输出,各位大哥看看是哪里有问题,指教指教,感激不尽,谢谢。
#include<stdio.h>
#include<malloc.h>

struct tree
{
int num;
struct tree *Lnext;
struct tree *Rnext;
}*head;

struct tree *insert(struct tree *p0,int x)
{
struct tree *p1;
if(p0==NULL)
{
p1=(struct tree *)malloc(sizeof(struct tree));
p1->num=x;p1->Lnext=NULL;p1->Rnext=NULL;
p0=p1;
}
else
{
if(x<p0->num)
{
insert(p0->Lnext,x);
}
else if(x>p0->num)
{
insert(p0->Rnext,x);
}
}return(head);
}

struct tree *LDR(struct tree *p0)
{
if(p0!=NULL)
{
LDR(p0->Lnext);
printf("%3d",p0->num);
LDR(p0->Rnext);
}
}

void ma

#include<stdio.h>
#include<malloc.h>

struct tree
{
int num;
struct tree *Lnext;
struct tree *Rnext;
}*head;

struct tree *insert(struct tree **p0,int x) /////////////就改了这个参数,原因是一级指针不能返回申请的内存,因为参数副本问题
{
struct tree *p1;
if(*p0==NULL)
{
p1=(struct tree *)malloc(sizeof(struct tree));
p1->num=x;p1->Lnext=NULL;p1->Rnext=NULL;
*p0=p1;
}
else
{
if(x<(*p0)->num)
{
insert(&((*p0)->Lnext),x);
}
else if(x>(*p0)->num)
{
insert(&((*p0)->Rnext),x);
}
}return(head);
}

struct tree *LDR(struct tree *p0)
{
if(p0!=NULL)
{
LDR(p0->Lnext);
printf("%3d",p0->num);
LDR(p0->Rnext);
}
return NULL;
}

void main()
{
int i,x;
head=NULL;