我用c写了一个二叉树的程序,windows没问题,ubuntu中cc过不了

软件和网站开发以及相关技术探讨
回复
oxangen
帖子: 64
注册时间: 2010-03-09 14:02
送出感谢: 0
接收感谢: 0

我用c写了一个二叉树的程序,windows没问题,ubuntu中cc过不了

#1

帖子 oxangen » 2010-04-28 22:53

我用c写了一个二叉树的程序,windows没问题,ubuntu中cc过不了,得改什么
#include<stdio.h>
#include<malloc.h>
typedef struct tree
{
char data;
struct tree *lchild;
struct tree *rchild;
}TREE;
TREE *CreateBittree()
{
char ch;
TREE *b;
ch=getchar();
if(ch=='@') b=NULL;
else
{
b=(TREE *)malloc(size(TREE));
b->data=ch;
b->lchild=CreateBittree();
b->rchild=CreateBittree();
}
return b;
}
void re_PreOrder(TREE *tree)
{
if(tree!=NULL){
printf("%c",tree->data);
re_PreOrder(tree->lchild);
re_PreOrder(tree->rchild);
}
}
void re_MidOrder(TREE *tree){
if(tree!=NULL){
re_MidOrder(tree->lchild);
printf("%c",tree->data);
re_MidOrder(tree->rchild);
}
}
void re_PostOrder(TREE *tree){
if(tree!=NULL)
re_PostOrder(tree->lchild);
re_PostOrder(tree->rchild);
printf("%c",tree->data);
}
void main(){
TREE *b;
printf("用先序输入一个二叉树,Input the bittree,using @ replace the empty child tree\n");
b=CreataBittree();
printf("先序序列为:\n");
re_PreOrder(&b);
printf("中序序列为:\n");
re_MidOrder(&b);
printf("后序序列为:\n");
}

然后cc的错误
zouvi@zouvi-laptop:Desktop$ gcc vim.c
vim.c: In function ‘CreateBittree’:
vim.c:17: error: expected expression before ‘TREE’
vim.c: In function ‘main’:
vim.c:48: warning: assignment makes pointer from integer without a cast
vim.c:50: warning: passing argument 1 of ‘re_PreOrder’ from incompatible pointer type
vim.c:24: note: expected ‘struct TREE *’ but argument is of type ‘struct TREE **’
vim.c:52: warning: passing argument 1 of ‘re_MidOrder’ from incompatible pointer type
vim.c:32: note: expected ‘struct TREE *’ but argument is of type ‘struct TREE **’

晕死,今天刚从机房做完的数据结构实验,会到ubuntu下就不行了,倍受打击
ThinkPad T60 with ubuntu 10.10 and windows xp sp3
头像
ptpt52
帖子: 717
注册时间: 2008-07-27 8:51
系统: Ubuntu/Windows
来自: 广西玉林|广东深圳
送出感谢: 1 次
接收感谢: 3 次
联系:

Re: 我用c写了一个二叉树的程序,windows没问题,ubuntu中cc过不了

#2

帖子 ptpt52 » 2010-04-29 6:50

写得错误一大堆 :em06
b=(TREE *)malloc(size(TREE)); // size 还是 sizeof ??

定义 CreateBittree
但下面引用 CreataBittree :em06 手误吧
……
走过去了也便有了路
http://www.ptpt52.com/
oxangen
帖子: 64
注册时间: 2010-03-09 14:02
送出感谢: 0
接收感谢: 0

Re: 我用c写了一个二叉树的程序,windows没问题,ubuntu中cc过不了

#3

帖子 oxangen » 2010-04-29 13:32

:em06
多谢提醒,错的不少,回来我是凭记忆些的,很多地方都错了,改后的程序如下
#include<stdio.h>
#include<malloc.h>
typedef struct tree
{
char data;
struct tree *lchild;
struct tree *rchild;
}TREE;
TREE *CreateBittree()
{
char ch;
TREE *b;
ch=getchar();
if(ch=='@') b=NULL;
else
{
b=(TREE *)malloc(sizeof(TREE));
b->data=ch;
b->lchild=CreateBittree();
b->rchild=CreateBittree();
}
return b;
}
void re_PreOrder(TREE *tree)
{
if(tree!=NULL){
printf("%c",tree->data);
re_PreOrder(tree->lchild);
re_PreOrder(tree->rchild);
}
}
void re_MidOrder(TREE *tree){
if(tree!=NULL){
re_MidOrder(tree->lchild);
printf("%c",tree->data);
re_MidOrder(tree->rchild);
}
}
void re_PostOrder(TREE *tree){
if(tree!=NULL)
re_PostOrder(tree->lchild);
re_PostOrder(tree->rchild);
printf("%c",tree->data);
}
void main(){
TREE *b;
printf("Input the bittree,using @ replace the empty child tree\n");
b=CreateBittree();
printf("\n先序序列为:");
re_PreOrder(b);
printf("\n中序序列为:");
re_MidOrder(b);
// printf("\n后序序列为:");
// re_PostOrder(b);
}

没有主函数的最后两行时没问题,运行结果也没问题,为什么加了后面两行后就有问题了呢?

这是没加入后两行后的显示结果:
zouvi@zouvi-laptop:Desktop$ gcc vim.c
zouvi@zouvi-laptop:Desktop$ ./a.out
Input the bittree,using @ replace the empty child tree
abd@e@@f@@cg@@@

先序序列为:abdefcg
中序序列为:debfagc
zouvi@zouvi-laptop:Desktop$

这是加入后的显示结果:
zouvi@zouvi-laptop:Desktop$ gcc vim.c
zouvi@zouvi-laptop:Desktop$ ./a.out
Input the bittree,using @ replace the empty child tree
abd@e@@f@@cg@@@

先序序列为:abdefcg
中序序列为:debfagc
段错误
zouvi@zouvi-laptop:Desktop$

试着用gbd调试(不是很会用)

zouvi@zouvi-laptop:Desktop$ gcc -g vim.c -o vim
zouvi@zouvi-laptop:Desktop$ gdb vim
GNU gdb (GDB) 7.0-ubuntu
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/zouvi/Desktop/vim...done.
(gdb) break main
Breakpoint 1 at 0x80485be: file vim.c, line 47.
(gdb) break re_PostOrder
Breakpoint 2 at 0x8048580: file vim.c, line 40.
(gdb) info break
Num Type Disp Enb Address What
1 breakpoint keep y 0x080485be in main at vim.c:47
2 breakpoint keep y 0x08048580 in re_PostOrder at vim.c:40
(gdb) r
Starting program: /home/zouvi/Desktop/vim

Breakpoint 1, main () at vim.c:47
47 printf("Input the bittree,using @ replace the empty child tree\n");
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/zouvi/Desktop/vim

Breakpoint 1, main () at vim.c:47
47 printf("Input the bittree,using @ replace the empty child tree\n");
(gdb)
晕,又回来了,这个gdb该怎么用的,怎么看程序哪错了呢,这个程序错哪了呢?
ThinkPad T60 with ubuntu 10.10 and windows xp sp3
oxangen
帖子: 64
注册时间: 2010-03-09 14:02
送出感谢: 0
接收感谢: 0

Re: 我用c写了一个二叉树的程序,windows没问题,ubuntu中cc过不了

#4

帖子 oxangen » 2010-04-29 13:43

结果是这样的
Breakpoint 2, main () at vim.c:47
47 printf("Input the bittree,using @ replace the empty child tree\n");
(gdb)
(gdb) n
Input the bittree,using @ replace the empty child tree
48 b=CreateBittree();
(gdb) n
abd@e@@f@@cg@@@
49 printf("\n先序序列为:");
(gdb) n

50 re_PreOrder(b);
(gdb) n
51 printf("\n中序序列为:");
(gdb) n
先序序列为:abdefcg
52 re_MidOrder(b);
(gdb) n
53 printf("\n后序序列为:");
(gdb) n
中序序列为:debfagc
54 re_PostOrder(b);
(gdb) n

Breakpoint 1, re_PostOrder (tree=0x804b008) at vim.c:40
40 if(tree!=NULL)
(gdb) n
41 re_PostOrder(tree->lchild);
(gdb) n

Breakpoint 1, re_PostOrder (tree=0x804b018) at vim.c:40
40 if(tree!=NULL)
(gdb) n
41 re_PostOrder(tree->lchild);
(gdb) n

Breakpoint 1, re_PostOrder (tree=0x804b028) at vim.c:40
40 if(tree!=NULL)
(gdb) n
41 re_PostOrder(tree->lchild);
(gdb) n

Breakpoint 1, re_PostOrder (tree=0x0) at vim.c:40
40 if(tree!=NULL)
(gdb) n
42 re_PostOrder(tree->rchild);
(gdb) n

Program received signal SIGSEGV, Segmentation fault.
0x08048597 in re_PostOrder (tree=0x0) at vim.c:42
42 re_PostOrder(tree->rchild);
(gdb)

最后的Program received signal SIGSEGV, Segmentation fault.
貌似re_PostOrder,这个程序写错了,改怎么改呢?
ThinkPad T60 with ubuntu 10.10 and windows xp sp3
回复

回到 “软件/网站开发”