This is a normal binary search traversal program...file name is BinaryFile.c
....
#include"bq.h" //****
typedef struct tree
{
int info;
struct tree *left;
struct tree *right;
}
tree; //line at which error is....previous definition of tree was here
tree *head;
createBST()
{
head=NULL;
}
inputBST(int element)
{
tree *node;
node=malloc(sizeof(tree));
tree *ptr;
ptr=head;
node->info=element;
node->left=NULL;
node->right=NULL;
if(head==NULL)
{
head=node;
}
else
{
while(ptr->left!=NULL || ptr->right!=NULL)
{
if(node->info < ptr->info)//means ptr it has to go to the left of the tree
{
if(ptr->left==NULL)
break;
ptr=ptr->left;
}
if(node->info > ptr->info)//this ptr means that it has to go to the right of the root
{
if(ptr->right==NULL)
break;
ptr=ptr->right;
}
}
if(node->info < ptr->info)//means ptr it has to go to the left of the tree
{
ptr->left=node;
}
if(node->info > ptr->info)//this ptr means that it has to go to the right of the root
{
ptr->right=node;
}
//ptr=head;
}
}
printBST_1()
{
tree *ptr;
ptr=head;
enqueue(ptr);
while(is_empty()!=0)
{
ptr=dequeue();
if(ptr->left!=NULL)
enqueue(ptr->left);
if(ptr->right!=NULL)
enqueue(ptr->right);
printf("%d ",ptr->info);
}
}
main()
{
int element,number;
printf("enter the no. of elements..\n");
scanf("%d",&number);
int i;
for(i=0;i<number;i++)
{
printf("\n-> ");
//element=gettint();
scanf("%d",&element);
inputBST(element);
}
//printBST(head);
printBST_1();
getch();
}
****and the file "bq.h" is....
#include "BinaryFile.c" //please note that appropriate include guard have been used....``
typedef struct queue
{
//int info;
tree *info;//tree node in queue....
struct queue *next;
}
q;
q *front;
q *rear;
createemptystack()
{
front=NULL;
rear=NULL;
}
enqueue(tree *element)
{
q *ptr;
ptr=malloc(sizeof(q));
if(front==NULL)
{
ptr->info=element;
ptr->next=NULL;
rear=ptr;
front=ptr;
}
else
{
ptr->info=element;
ptr->next=NULL;
rear->next=ptr;
rear=ptr;
}
}
tree * dequeue()
{
q *ptr;
ptr=malloc(sizeof(q));
ptr=front;
front=front->next;
return ptr;
free(ptr);
}
tree * peek()
{
tree *x;
x=rear->info;
return x;
}
int is_empty()
{
if(head==NULL)
return 0;
else
return 1;
}
What I am doing here is using a queue file (bq) to do level-order traversal of the binary search tree made by the user in BinaryFile.h
.
In the file BinaryFile.h
(the first code) the compiler for each function declared in it shows
error: redefinition of (function name)
error: previous definition of (function name) was here
Please explain why this error is coming and how can I solve it.