免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2790 | 回复: 12
打印 上一主题 下一主题

[C] 请大家帮我看看这个二叉树的问题出在哪里啊? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-03-15 15:16 |只看该作者 |倒序浏览
会出现段错误,不知道是哪里问题。
程序要求创建一个二叉树,并按前序、中序、后序的方式显示。

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef struct Tnode {
  4.         struct Tnode * left;
  5.         struct Tnode * right;
  6.         int tag;
  7.         char data;
  8. }* node;

  9. void CreateBintree(node Lnode)
  10. {
  11.         char e;
  12.         Lnode = (node) malloc(sizeof (struct Tnode));
  13.         printf ("size of Lnode = %d\n", sizeof(*Lnode));
  14.         printf ("size of bool %d\n", sizeof (Lnode -> tag));
  15.         printf ("First Lnode malloc complete! \n");
  16.         scanf("%s",&e);
  17.         if (e == '.') {
  18.                 Lnode -> tag = 0;
  19.                 printf ("Lnode tag : 0 \n");
  20.                 return;
  21.         }
  22.         else Lnode -> tag = 1;
  23.         Lnode -> data = e;
  24.         printf ("Lnode.data : %c\n", Lnode -> data);
  25.         printf ("scanf First complete! \n ");
  26.         CreateBintree(Lnode -> left);
  27.         printf ("Create left\n");
  28.         CreateBintree(Lnode -> right);
  29.         printf ("Create right\n");
  30.         return;
  31. }
  32.        
  33. void PreBintree(node Lnode)
  34. {
  35.         if (!Lnode->tag) return;
  36.         printf (" %d ",Lnode -> data);
  37.         PreBintree(Lnode -> left);
  38.         PreBintree(Lnode -> right);
  39.         return;
  40. }

  41. void MidBintree(node Lnode)
  42. {
  43.         if (!Lnode->tag) return;
  44.         PreBintree(Lnode -> left);
  45.         printf (" %d ",Lnode -> data);
  46.         PreBintree(Lnode -> right);
  47.         return;
  48. }

  49. void PastBintree(node Lnode)
  50. {
  51.         if (!Lnode->tag) return;
  52.         PreBintree(Lnode -> left);
  53.         PreBintree(Lnode -> right);
  54.         printf (" %d ",Lnode -> data);
  55.         return;
  56. }

  57. int main (void)
  58. {
  59.         char * hello="Hello,World! Now , It is bintree!";
  60.         printf ("%s\n",hello);
  61.         node Bintree = (node) malloc (sizeof (struct Tnode));;
  62.         printf ("malloc Bintree complete! \n");
  63.         printf ("Bintree address: %x \n", Bintree);
  64.         CreateBintree(Bintree);
  65.         printf ("Pre Order:\t");
  66.         PreBintree(Bintree);
  67.         MidBintree(Bintree);
  68.         PastBintree(Bintree);
  69.         return 0;
  70. }
复制代码

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2008-03-15 16:07 |只看该作者
>>CreateBintree(Bintree)
你应该用指针的指针来传递参数。
如果不明白,请搞清楚c语言参数传递的机制。

论坛徽章:
0
3 [报告]
发表于 2008-03-15 16:52 |只看该作者
刚才想了一下,但是还没有明白。
我觉得 Bintree 本身就是个指向结构的指针,传递Bintree的时候,传递的就是地址,函数里应该可以对传入的地址操作吧
为什么还要传递指针的指针呢?

论坛徽章:
0
4 [报告]
发表于 2008-03-15 17:12 |只看该作者
应该不是指针的错误
char e
scanf("%s",&e);  ////////////////是否这里,scanf("%c",&e)

论坛徽章:
0
5 [报告]
发表于 2008-03-15 17:21 |只看该作者
你在create时将Lnode重新分配了内存,只是改变了Lnode的指向,但这并没有改变到Bintree的指向。只有传Bintree的地址进去才可以。

论坛徽章:
0
6 [报告]
发表于 2008-03-15 17:34 |只看该作者
改了几个地方。 但是新的问题又出来了,
如果只输入一个“.”,就马上结束,这是正常的。
但是如果先输入一个数字,再输入“.”,不正常了,我是输入一个数据就敲一下回车的。

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef struct Tnode {
  4.         struct Tnode * left;
  5.         struct Tnode * right;
  6.         int tag;
  7.         char data;
  8. }* node;

  9. void CreateBintree(node Lnode)
  10. {
  11.         char e;
  12.         printf ("size of Lnode = %d\n", sizeof(*Lnode));
  13.         scanf("%c",&e);
  14.         if (e == '.') {
  15.                 Lnode -> tag = 0;
  16.                 printf ("Lnode tag : %d \n", Lnode -> data);
  17.                 return;
  18.         }
  19.         else Lnode -> tag = 1;
  20.         Lnode -> data = e;
  21.         printf ("Lnode.data : %c\n", Lnode -> data);
  22.         printf ("scanf First complete! \n ");
  23.         Lnode -> left = (node) malloc (sizeof(struct Tnode));
  24.         CreateBintree(Lnode -> left);
  25.         printf ("Create left\n");
  26.         Lnode -> right = (node) malloc (sizeof(struct Tnode));
  27.         CreateBintree(Lnode -> right);
  28.         printf ("Create right\n");
  29.         return;
  30. }
  31.        
  32. void PreBintree(node Lnode)
  33. {
  34.         if (!Lnode->tag) return;
  35.         printf (" %d ",Lnode -> data);
  36.         PreBintree(Lnode -> left);
  37.         PreBintree(Lnode -> right);
  38.         return;
  39. }

  40. void MidBintree(node Lnode)
  41. {
  42.         if (!Lnode->tag) return;
  43.         PreBintree(Lnode -> left);
  44.         printf (" %d ",Lnode -> data);
  45.         PreBintree(Lnode -> right);
  46.         return;
  47. }

  48. void PastBintree(node Lnode)
  49. {
  50.         if (!Lnode->tag) return;
  51.         PreBintree(Lnode -> left);
  52.         PreBintree(Lnode -> right);
  53.         printf (" %d ",Lnode -> data);
  54.         return;
  55. }

  56. int main (void)
  57. {
  58.         char * hello="Hello,World! Now , It is bintree!";
  59.         printf ("%s\n",hello);
  60.         node Bintree = (node) malloc (sizeof(struct Tnode));
  61.         printf ("malloc Bintree complete! \n");
  62.         CreateBintree(Bintree);
  63.         printf ("\nPre Order:\t");
  64.         PreBintree(Bintree);
  65.         printf ("\nMid Order:\t");
  66.         MidBintree(Bintree);
  67.         printf ("\nPast Order:\t");
  68.         PastBintree(Bintree);
  69.         return 0;
  70. }
复制代码

论坛徽章:
0
7 [报告]
发表于 2008-03-15 18:08 |只看该作者
原帖由 liec 于 2008-3-15 17:34 发表
改了几个地方。 但是新的问题又出来了,
如果只输入一个“.”,就马上结束,这是正常的。
但是如果先输入一个数字,再输入“.”,不正常了,我是输入一个数据就敲一下回车的。

#include
#include

ty ...

1. 二楼指出的问题你没有修改,还有main函数里的内存分配没必要;
2. 可以用两个getchar代替scanf输入字符(一个读取真正的字符,另一个读取换行符)

论坛徽章:
0
8 [报告]
发表于 2008-03-15 18:34 |只看该作者
原帖由 liec 于 2008-3-15 16:52 发表
刚才想了一下,但是还没有明白。
我觉得 Bintree 本身就是个指向结构的指针,传递Bintree的时候,传递的就是地址,函数里应该可以对传入的地址操作吧
为什么还要传递指针的指针呢?


这是我刚才理解的,现在还没有想明白呢

再一个就是main中的内存分配如果没有,在CreateBintree()中也是没有内存分配的啊。

论坛徽章:
0
9 [报告]
发表于 2008-03-15 18:51 |只看该作者
二楼 说的正解
在函数内部对变量本身的修改是无效的  这是基础
假如对指针指向的内容修改 才是有效的

论坛徽章:
0
10 [报告]
发表于 2008-03-15 19:33 |只看该作者
我的这个程序应该只是把地址传进去,然后对地址所指向的结构体进行修改吧?
不知道我说得对不对
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP