| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 963 人关注过本帖
标题:二叉链表的建立出错
只看楼主 加入收藏
Karryu
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2016-5-5
结帖率:90%
收藏
已结贴  问题点数:15 回复次数:4 
二叉链表的建立出错
在中序遍历函数的这一步ptr=ptr->lch;开始就执行不下去了
是不是二叉链表没有建立起来
程序代码:
#include <stdio.h>
#include <malloc.h>
#define N 10

struct node
{
    int data;
    struct node *lch;
    struct node *rch;
};
typedef struct node bitree; 

bitree *prec_bt(bitree *bt)
{
    int a;
    printf("请输入结点的值:\n");
    scanf("%d",&a); 
    if(a==0) 
    {
         bt=(bitree *)malloc(sizeof(bitree));
        bt=NULL;
    }
    else
    {
        bt=(bitree *)malloc(sizeof(bitree));
        bt->data=a;
        prec_bt(bt->lch);
        prec_bt(bt->rch);

    }
    return bt;
}

bitree *In_bt(bitree *root)
{
    bitree *Ss[N];
    bitree *ptr;
    int Ss_top=-1;
    ptr=prec_bt(root);
    printf("二叉树各结点的值为:\n");
    while((ptr!=NULL)||(Ss_top!=-1))
    {
        while(ptr!=NULL)
        {
        Ss_top++;
        Ss[Ss_top]=ptr;
         ptr=ptr->lch;
        }
         if(Ss_top!=-1)
        {
        ptr=Ss[Ss_top];
        printf("%d",ptr->data);
        Ss_top--;
        ptr=ptr->rch;
        }
    }

 printf("over");

 return root;
}
    
main()
{
    bitree *root;
    In_bt(root);    
}
2016-10-29 16:18
word123
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:13
帖 子:333
专家分:1622
注 册:2014-4-5
收藏
得分:7 
#include <stdio.h>
#include <malloc.h>
#define N 10

struct node
{
    int data;
    struct node *lch;
    struct node *rch;
};
typedef struct node bitree;

bitree *prec_bt(bitree *&bt)//就改了这里
{
    int a;
    printf("请输入结点的值:\n");
    scanf("%d",&a);
    if(a==0)
    {
         bt=(bitree *)malloc(sizeof(bitree));
        bt=NULL;
    }
    else
    {
        bt=(bitree *)malloc(sizeof(bitree));
        bt->data=a;
        prec_bt(bt->lch);
        prec_bt(bt->rch);

    }
    return bt;
}

bitree *In_bt(bitree *root)
{
    bitree *Ss[N];
    bitree *ptr;
    int Ss_top=-1;
    ptr=prec_bt(root);
    printf("二叉树各结点的值为:\n");
    while((ptr!=NULL)||(Ss_top!=-1))
    {
        while(ptr!=NULL)
        {
        Ss_top++;
        Ss[Ss_top]=ptr;
         ptr=ptr->lch;
        }
         if(Ss_top!=-1)
        {
        ptr=Ss[Ss_top];
        printf("%d",ptr->data);
        Ss_top--;
        ptr=ptr->rch;
        }
    }
 printf("over");
 return root;
}
   
main()
{
    bitree *root;
    In_bt(root);   
}
2016-10-29 22:30
书生牛犊
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:星夜征程
等 级:贵宾
威 望:10
帖 子:1101
专家分:5265
注 册:2015-10-27
收藏
得分:8 
@题主:对的你的链表确实没建立起来,因为C语言的函数参数只传递值。
main()
{
    bitree *root;
    In_bt(root);  

}
执行In_bt并不能修改到main里面的root的值,除非你把root的地址传进去(就像我们要在函数里修改int之类的数据就需要传地址一样,bittree*也一样),函数里面也需要对应修改一下

@word123:您的方法修改很少,但是引用这个概念貌似是C++的。。。
图片附件: 游客没有浏览图片的权限,请 登录注册

从解决问题的层面考虑这是很好地方法,但是如果希望深入了解锻炼C指针的运用,可能还是要麻烦一点。。




φ(゜▽゜*)♪
2016-10-30 00:16
Karryu
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2016-5-5
收藏
得分:0 
回复 3楼 书生牛犊
因为我刚学没多久 不懂什么意思 怎么才可以实现把地址传过去
2016-10-31 23:43
Karryu
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2016-5-5
收藏
得分:0 
回复 3楼 书生牛犊
程序代码:
#include <stdio.h>
#include <malloc.h>
#define N 10

struct node
{
    int data;
    struct node *lch;
    struct node *rch;
};
typedef struct node bitree; 

bitree *prec_bt()
{
    bitree *bt;
    int a;
    printf("请输入结点的值:\n");
    scanf("%d",&a); 
    if(a==0) 
    {
         bt=(bitree *)malloc(sizeof(bitree));
        bt=NULL;
    }
    else
    {
        bt=(bitree *)malloc(sizeof(bitree));
        bt->data=a;
        bt->lch=prec_bt();
        bt->rch=prec_bt();

    }
    return(bt);
}

In_bt(bitree *root)
{
    bitree *Ss[N];
    bitree *ptr;
    int Ss_top;
    Ss_top=-1;
    ptr=root;    
    do
    {
        while(ptr!=NULL)
        {
        Ss_top++;
        Ss[Ss_top]=ptr;
         ptr=ptr->lch;
        }
         if(Ss_top!=-1)
        {
        ptr=Ss[Ss_top];
        printf("%d\t",ptr->data);
        Ss_top--;
        ptr=ptr->rch;
        }
    }while((ptr!=NULL)||(Ss_top!=-1));
}


int count(bitree *bt)              //计算叶子结点个数
{
    static int y=0;
    if(bt!=NULL)
    {count(bt->lch);
     count(bt->rch);
     if(bt->lch==NULL&&bt->rch==NULL)    //当左子树和右子树均无时,为叶子节点
        {y++;
     printf("%d",y);}
    }
return y;
    }

    
main()
{
    bitree *root;
    int leaves;
     root=prec_bt();
    printf("二叉树中序遍历结果为:\n");
    In_bt(root);
    printf("\ncount_leaves number:");
    leaves=count(root);
    printf("%d",leaves);
    system("pause");
}


现在前面的都没有问题了 为什么在计算叶子结点这一步
     if(bt->lch==NULL&&bt->rch==NULL)    //当左子树和右子树均无时,为叶子节点
        {y++;
     printf("%d",y);}
    }
发现是从来没有运行到的,就是不会去判断
2016-11-05 23:07
快速回复:二叉链表的建立出错
数据加载中...
 
   



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

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