【发布时间】:2016-07-14 14:03:17
【问题描述】:
我编写了一个 C 程序来输入二叉搜索树的元素并显示其 InOrder、PostOrder 和 PreOrder 遍历。
#include<stdio.h>
#include<stdlib.h>
struct tnode
{
int data;
struct tnode *leftc;
struct tnode *rightc;
};
int main()
{
char ans='N';
struct tnode *new_node,*root;
// struct tnode *get_node();
root=NULL;
do{
// new_node=get_node();
printf("\nEnter the Element");
scanf("%d",&new_node->data);
if(root==NULL)
root=new_node;
else
insert(root,new_node);
printf("\nDo you want to enter a new element?(y/n)");
scanf("%c",&ans);
}while(ans == 'y');
printf("Inorder traversal:the elements in the tree are");
inorder(root);
printf("\nPreorder traversal:the elements in the tree are");
preorder(root);
printf("Postorder traversal:the elements in the tree are");
postorder(root);
return 0;
}
void insert(struct tnode ** tree,int num)
{
struct tnode *temp = NULL;
if(!(*tree))
{
temp=(struct tnode *)malloc(sizeof (struct tnode));
temp->leftc=temp->rightc=NULL;
temp->data=num;
*tree=temp;
return;
}
if(num < (*tree)->data)
{
insert(&(*tree)->leftc,num);
}
else if(num > (*tree)->data)
{
insert(&(*tree)->rightc,num);
}
}
void preorder(struct tnode * s)
{
if(s)
{
printf("%d\n",s->data);
preorder(s->leftc);
preorder(s->rightc);
}
}
void inorder(struct tnode * s)
{
if(s)
{
inorder(s->leftc);
printf("%d\n",s->data);
inorder(s->rightc);
}
}
void postorder(struct tnode * s)
{
if(s)
{
postorder(s->leftc);
postorder(s->rightc);
printf("%d\n",s->data);
}
}
我收到以下警告消息:
warning: implicit declaration of functionS,
conflicting types OF FUNCTIONS,
new_node’ may be used uninitialized in this function
我无法理解这些错误。你能帮我解决这些问题吗?
【问题讨论】:
-
代码请缩进,否则我就不看了。
-
没有看代码,但关于第一个警告:在 C 中,您必须在使用 therm 之前声明函数。事实上,你必须在使用它之前声明everything。至于其他的,
new_node变量在使用前有没有初始化?
标签: c inorder preorder postorder