求助算法编译问题

发布于 2022-10-15 09:05:54 字数 6329 浏览 24 评论 0

gcc4.2.4  Ubuntu 下的编译器
下列代码……报错

#include "stdlib.h"
#include "stdio.h"
#define ERROR 0
#define OVERFLOW -2
#define OK 1
typedef int Status;
typedef char ElemType ;
typedef struct BiTree{
        ElemType  data;
        struct BiTree *lchild, *rchild;
} *BiTree,BIT;

Status CreateBiTree(BiTree T)
{ char ch;
   scanf("%c",&ch);
   if(ch=='*') T=NULL;
   else {
                        if(!(T=(BiTree)malloc(sizeof(BIT))))  exit(OVERFLOW);
                        T->data=ch;
                        CreateBiTree(T->lchild);
                        CreateBiTree(T->rchild);
                        };
                        return OK;
                        };
int Visit (ElemType e)
{       
        printf("%c",e);
        return OK;
};
Status PreOrderTraverse ( T){

if(T){
        if(Visit(T->data))
                if(PreOrderTraverse(T->lchild))
                        if(PreOrderTraverse(T->rchild)) return OK;
                        return ERROR;
} else  return OK;
};

Status InOrderTraverse ( T){

if(T){
        if(InOrderTraverse(T->lchild))
                if(Visit(T->data))       
                        if(InOrderTraverse(T->rchild)) return OK;
                        return ERROR;
} else  return OK;
};
Status PostderTraverse (T){

if(T){
                if(PostOrderTraverse(T->lchild))
                        if(Visit(T->data))
                                if(PostOrderTraverse(T->rchild)) return OK;
                        return ERROR;
} else  return OK;
};

main()
{
  CreateBiTree ();
  PreOrderTraverse ();
        InOrderTraverse();
        PostderTraverse();
}

错误代码
20091176@calcium:~/project$ gcc -o tree tree.c
In file included from /usr/include/stdio.h:34,
                 from tree.c:2:
tree.c:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before string constant
In file included from /usr/include/stdio.h:75,
                 from tree.c:2:
/usr/include/libio.h:332: error: expected specifier-qualifier-list before ‘size_t’
/usr/include/libio.h:364: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/libio.h:373: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/libio.h:493: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘_IO_sgetn’
In file included from tree.c:2:
/usr/include/stdio.h:312: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdio.h:319: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdio.h:361: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdio.h:363: error: format string argument not a string type
/usr/include/stdio.h:365: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdio.h:367: error: format string argument not a string type
/usr/include/stdio.h:678: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘fread’
/usr/include/stdio.h:684: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘fwrite’
/usr/include/stdio.h:706: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘fread_unlocked’
/usr/include/stdio.h:708: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘fwrite_unlocked’
tree.c: In function ‘CreateBiTree’:
tree.c:19: warning: incompatible implicit declaration of built-in function ‘malloc’
tree.c:19: warning: incompatible implicit declaration of built-in function ‘exit’
tree.c: In function ‘PreOrderTraverse’:
tree.c:34: error: invalid type argument of ‘->’
tree.c:35: error: invalid type argument of ‘->’
tree.c:36: error: invalid type argument of ‘->’
tree.c: In function ‘InOrderTraverse’:
tree.c:44: error: invalid type argument of ‘->’
tree.c:45: error: invalid type argument of ‘->’
tree.c:46: error: invalid type argument of ‘->’
tree.c: In function ‘PostderTraverse’:
tree.c:53: error: invalid type argument of ‘->’
tree.c:54: error: invalid type argument of ‘->’
tree.c:55: error: invalid type argument of ‘->’
tree.c: In function ‘main’:
tree.c:62: error: too few arguments to function ‘CreateBiTree’

求指教

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橪书 2022-10-22 09:05:54

写的真乱。

Status PreOrderTraverse ( T){

这儿的T是神马?

来日方长 2022-10-22 09:05:54

函数结束的}后为嘛加个;?

世态炎凉 2022-10-22 09:05:54

不光语法上有问题,逻辑上也混乱:CreateBiTree,这个函数你没有返回一个节点,无论是通过返回值还是通过参数输出。
懒得看了。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文