急问一指针问题!在线等!

onlyforheli 2003-10-10 12:47:22
这是一个输入数据,建立二叉排序树的程序:
#include<stdio.h>
#include<stdlib.h>

struct node
{
struct node *left, *right;
int data;
int l,r;
};

void view(struct node *);
void insert(struct node *, struct node *);

struct node *Head = NULL;
void main(void)
{
int num;
char cha;
struct node *up, *p;
clrscr();
printf("Please input a serie of numbers end of ;:\n");
do{
scanf("%d", &num);
cha = getchar();
INSERT(Head, num);
}
while(cha != ';');
view(Head);
getchar();
getchar();
}

void INSERT(struct node *h, int num)
{
struct node *p = (struct node *)malloc(sizeof(struct node));
p->left = NULL;
p->right = NULL;
p->l = 0;
p->r = 0;
p->data = num;
insert(h, p);
}

void insert(struct node *h, struct node *p)
{
if(h == NULL)
{
h = p;
}
else
{
if(h->data > p->data)
{
h->l = 1;
insert(h->left, p);
}
else
{
h ->r = 1;
insert(h->right, p);
}
}
}

问题是:INSERT函数和insert函数中,Head作为参数进行调用,却没法建立树,请解答。
...全文
51 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
aflyinghorse 2003-10-10
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>

struct node
{
struct node *left, *right;
int data;
int l,r;
};

void view(struct node * p)
{
if (p != NULL)
{
view(p->left);
printf("%d ",p->data);
view(p->right);
}
};
void insert(struct node **, struct node *);
void INSERT(struct node **, int);


int main()
{
int num;
char cha;
struct node *Head = NULL;
// struct node *up, *p;
// clrscr();
printf("Please input a serie of numbers end of ;:\n");
do{
scanf("%d", &num);
cha = getchar();
INSERT(&Head, num);
}
while(cha != ';');
view(Head);
getchar();
getchar();
}

void INSERT(struct node **h, int num)
{
struct node *p = (struct node *)malloc(sizeof(struct node));
p->left = NULL;
p->right = NULL;
p->l = 0;
p->r = 0;
p->data = num;
insert(h, p);
}

void insert(struct node **h, struct node *p)
{
if(*h == NULL)
{
*h = p;
}
else
{
if((*h)->data > p->data)
{
(*h)->l = 1;
insert(&((*h)->left), p);
}
else
{
(*h)->r = 1;
insert(&((*h)->right), p);
}
}
}

输入:
Please input a serie of numbers end of ;:
3
2
1;
输出:
1 2 3
dddd8888 2003-10-10
  • 打赏
  • 举报
回复
void insert(struct node *h, struct node p)
{
if( NULL == h)
{
h = &p;
}
SlaSk 2003-10-10
  • 打赏
  • 举报
回复
void insert(struct node *h, struct node p)
{
if(h == NULL)
{
h = &p;
}

SlaSk 2003-10-10
  • 打赏
  • 举报
回复
void insert(struct node **h, struct node *p)
{
if(*h == NULL)
{
*h = p;
}



void insert(struct node *h, struct node p)
{
if(*h == NULL)
{
h = &p;
}


??
leyt 2003-10-10
  • 打赏
  • 举报
回复
仅同意 sharkhuang(什么都不懂,什么都想学!)

其他人恐怕没仔细读此程序
leyt 2003-10-10
  • 打赏
  • 举报
回复
应该使用指针的指针或引用(C++)进行传值才能改变HEAD的值

因为HEAD是个指针
sharkhuang 2003-10-10
  • 打赏
  • 举报
回复
错误!你要传h的地址进去才能给head负值!
void insert(struct node **h, struct node *p)
{
if(*h == NULL)
{
*h = p;
}
daizh 2003-10-10
  • 打赏
  • 举报
回复
void main(void)
{
int num;
char cha;
struct node *up, *p;
Head = (struct node *)malloc(sizeof(struct node));//添加初始化
clrscr();
printf("Please input a serie of numbers end of ;:\n");
do{
scanf("%d", &num);
cha = getchar();
INSERT(Head, num);
}
while(cha != ';');
view(Head);
getchar();
getchar();
}
sharkhuang 2003-10-10
  • 打赏
  • 举报
回复
第一次
if(h == NULL)
{
h = p;
}
不就是负值了吗?
tigerhohoo 2003-10-10
  • 打赏
  • 举报
回复
Head 没有赋值,恒为NULL.在main中的do之前给Head分配内存.

69,382

社区成员

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

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