| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 710 人关注过本帖
标题:请教一下
只看楼主 加入收藏
jinxilee
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2008-4-6
收藏
 问题点数:0 回复次数:6 
请教一下
创造二叉排序树  第一个地址 和第二个的差值 和后面的不一样?
struct node *create()
{struct node *p,*r,*b,*f;
b=(struct node*)malloc(sizeof(struct node));
printf("input the nums of tree:(input -1 to stop)\n");
scanf("%d",&b->data);
b->lc=NULL;
b->rc=NULL;
r=NULL;
n=0;
while(b->data!=-1)
{n=n+1;
  if(n==1){r=b;p=r;}
  else {while(p!=NULL)
    {if(b->data<p->data) {f=p;p=p->lc;}
     else {f=p;p=p->rc;}
    }
       }
  f=b;
  p=r;
  printf("p_value:%ld\n",f);
  b=(struct node*)malloc(sizeof(struct node));
  scanf("%d",&b->data);
}
return(r);
free(b);
}
void main()
{struct node *r;
printf("now ,start to create the tree:\n");
r=create();
}
搜索更多相关主题的帖子: node struct data NULL 
2008-04-20 10:22
flyue
Rank: 10Rank: 10Rank: 10
来 自:江南西道
等 级:贵宾
威 望:19
帖 子:3465
专家分:1563
注 册:2006-6-20
收藏
得分:0 
Sorry!
node 你没给代码,
n没有定义

天之道,损有余而补不足.人之道则不然,损不足以奉有余.孰能有余以奉天下,唯有道者.
2008-04-20 10:26
jinxilee
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2008-4-6
收藏
得分:0 
创造二叉排序树  第一个地址 和第二个的差值 和后面的不一样?
#include<stdio.h>
struct node
{int data;
 struct node *lc;
 struct node *rc;
};
int n;

struct node *create()
{struct node *p,*r,*b,*f;
 b=(struct node*)malloc(sizeof(struct node));
 printf("input the nums of tree:(input -1 to stop)\n");
 scanf("%d",&b->data);
 b->lc=NULL;
 b->rc=NULL;
 r=NULL;
 n=0;
while(b->data!=-1)
 {n=n+1;
  if(n==1){r=b;p=r;}
  else {while(p!=NULL)
    {if(b->data<p->data) {f=p;p=p->lc;}
     else {f=p;p=p->rc;}
    }
       }
  f=b;
  p=r;
  printf("p_value:%ld\n",f);
  b=(struct node*)malloc(sizeof(struct node));
  scanf("%d",&b->data);
 }
 return(r);
 free(b);
}
2008-04-20 10:29
jinxilee
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2008-4-6
收藏
得分:0 
还是一样的结果,不行啊
2008-04-20 10:30
flyue
Rank: 10Rank: 10Rank: 10
来 自:江南西道
等 级:贵宾
威 望:19
帖 子:3465
专家分:1563
注 册:2006-6-20
收藏
得分:0 
程序代码:
#include <stdio.h>
#include <malloc.h>

struct node
{
    int            data;
    struct node *lc;
    struct node *rc;
};

int n;

struct node *create()
{
    struct node *p, *r, *b, *f;
    b = (struct node*)malloc(sizeof(struct node));
    printf("input the nums of tree:(input -1 to stop)\n");
    scanf("%d", &b->data);
    b->lc = NULL;
    b->rc = NULL;
    r = NULL;
    n = 0;
    while(b->data != -1)
    {
        n = n + 1;
        if(n == 1)
        {
            r = b;
            p = r;
        }
        else 
        {
            while(p != NULL)
            {
                if(b->data < p->data) 
                {
                    f = p;
                    p = p->lc;
                }
                else 
                {
                    f = p;
                    p = p->rc;
                }
            }
        }
        f = b;
        p = r;
        printf("p_value:%ld\n", f->data);/*这里我修改的.你运行一下看看是不是你想要的结果*/
        b = (struct node*)malloc(sizeof(struct node));
        scanf("%d", &b->data);
    }
    return(r);
    free(b);
}

void main()
{
    struct node *r;
    printf("now ,start to create the tree:\n");
    r = create();
}

天之道,损有余而补不足.人之道则不然,损不足以奉有余.孰能有余以奉天下,唯有道者.
2008-04-20 10:36
jinxilee
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2008-4-6
收藏
得分:0 
题意 是输出指针值。。刚才赶了一下,加 注释
#include<stdio.h>
struct node
{int data;
 struct node *lc;
 struct node *rc;
};
int n;

struct node *create()
{struct node *p,*r,*b,*f;/*r为根结点,b为要插入的结点,p为扫面指针,f用于保存p*/
 b=(struct node*)malloc(sizeof(struct node));/*开辟一个新结点*/
 printf("input the nums of tree:(input -1 to stop)\n");
 scanf("%d",&b->data);
 b->lc=NULL;
 b->rc=NULL;
 r=NULL;/*对r初始化*/
 n=0;
while(b->data!=-1)/*当输入-1时t时结束循环*/
 {n=n+1;
  if(n==1){r=b;p=r;}/*使 第一次输入的作为根结点*/
  else {while(p!=NULL)
    {if(b->data<p->data) {f=p;p=p->lc;}
     else {f=p;p=p->rc;}/*用f保存p,p再 指向下一个*/
    }
       }
  f=b;/*f就是指向输入的结点*/
  p=r;/*使p重新回到根结点*/
  printf("p_value:%ld\n",f);/*打印插入结点的指针值*/
  b=(struct node*)malloc(sizeof(struct node));
  scanf("%d",&b->data);/*再次输入一个新的结点*/
 }
 return(r);
 free(b);
}
void main()
{struct node *r;
 printf("now ,start to create the tree:\n");
 r=create();
}

[[it] 本帖最后由 jinxilee 于 2008-4-20 10:54 编辑 [/it]]
2008-04-20 10:52
jinxilee
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2008-4-6
收藏
得分:0 
帮帮忙啊!
2008-04-20 11:08
快速回复:请教一下
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012003 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved