我只是想编写一个简单的二叉搜索树程序,用户可以在其中插入节点并以中序、前序或后序模式查看树中的所有节点。我的代码是
#include <stdio.h>
#include <stdlib.h>
struct treenode
{
int data;
struct treenode *lchild;
struct treenode *rchild;
}*root;
void insertnode(struct treenode *p,int d)
{
if(p==NULL)
{
// means the tree is empty
p=(struct treenode *)malloc(sizeof(struct treenode));
p->data=d;
p->lchild=NULL;
p->rchild=NULL;
}
else
{
// start comparing the new data from root
if( d<p->data )
insertnode((&(p->lchild)),d);
else
insertnode((&(p->lchild)),d);
}
}
void preorder(struct treenode *p)
{
if(p==NULL)
{
printf("\nThe list is empty");
}
else
{
printf("%d",p->data);
preorder(p->lchild);
preorder(p->rchild);
}
}
void postorder(struct treenode *p)
{
if(p==NULL)
{
printf("\nThe list is empty");
}
else
{
preorder(p->lchild);
preorder(p->rchild);
printf("%d",p->data);
}
}
void inorder(struct treeode *p)
{
if(p==NULL)
{
printf("\nThe list is empty");
}
else
{
preorder(p->lchild);
printf("%d",p->data);
preorder(p->rchild);
}
}
int main(void)
{
root=NULL;
int choice,data;
while(1)
{
printf("\nPress 1 for inserting a node in BST fashion: ");
printf("\nPress 2 for traversing the tree in preorder fashion :");
printf("\nPress 3 for traversing the tree in postorder fashion :");
printf("\nPress 4 for traversing the tree in inorder fashion :");
printf("\nPress 5 to exit :");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: printf("\nEnter the data to be inserted:");
scanf("%d",&data);
insertnode(root,data);
break;
case 2: preorder(root);
break;
case 3: postorder(root);
break;
case 4: inorder(root);
break;
case 5: exit(0);
break;
default: printf("\nYou have enetred an invalid choice. Please try again");
}
}
return 0;
}
有一堆错误信息说
dereferencing pointer to incomplete type
问题是什么 ?另外我对双间接指针不太满意,所以有人可以解释一下我如何传递和检索双间接指针(如果我需要在上面的程序中传递它们)。