-1

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Binary_Mode
  • 13
  • 1
  • 7
  • 5
    You're including the C file in the H file and the H file in the C file. That's never going to work. Put all of the your definitions in the H file all of the code in the C file, include the H file in the C file (not both) and add include guards to the H file. – twain249 May 13 '17 at 19:17
  • Almost all of your functions are missing a return type. This has been invalid since C99. In C89 it means you're using implicit `int`, so you're missing a lot of `return` statements, too. – melpomene May 13 '17 at 20:37

1 Answers1

1

Your code structure is slightly crazy. In general, you should never include a .c file, especially not in a header. What you're doing is even more confusing: You're including bq.h in BinaryFile.c and BinaryFile.c in bq.h, forming an infinite loop of includes.

What you should do instead is ... well, it depends. This code seems simple enough that you can just put it all in a single .c file and be done with it.

If you want to structure your code in a way that uses multiple files, then you should think about modules, i.e. units of related functionality. For example, you could have a module that defines a binary tree type and functions that work on that tree. For each such module, you would write a .h file that describes the module interface and a .c file that implements the interface.

Your code could be split into three parts: A binary tree module, a queue module, and a main program.

You would have a binary tree header, tree.h:

#ifndef TREE_H_
#define TREE_H_

typedef struct tree
{
    int info;
    struct tree *left;
    struct tree *right;
}
tree;

void createBST(void);
void inputBST(int);
void printBST_1(void);

#endif

Note that the header only contains type definitions and function declarations (if you decide to use global variables in your interface, you would also put variable declarations here (but you should avoid global variables where possible)).

A corresponding implementation file, tree.c:

// important: include our own interface definition to make sure it matches our implementation
#include "tree.h"
#include <stdlib.h>

// private functions/variables (those not part of our interface) are marked 'static'
static tree *head;

void createBST(void) {
    ...  // your code goes here
}

void inputBST(int) {
    ... // your code goes here
}

void printBST_1(void) {
    ... // your code goes here
}

Similarly, for queues you would have a bq.h:

#ifndef BQ_H_
#define BQ_H_

#include "tree.h"

typedef struct queue
{
    //int info;
    tree *info; //tree node in queue....
    struct queue *next;
}
queue;

void createemptystack(void);
void enqueue(tree *);
tree *dequeue(void);
tree *peek(void);
int is_empty(void);

#endif

And a corresponding bq.c:

#include "bq.h"

static queue *front;
static queue *rear;

void createemptystack(void) {
    ...  // your code goes here
}

...  // other function definitions go here

Finally, you have main.c:

#include "tree.h"
#include "bq.h"
#include <stdio.h>

int main(void) {
    ...  // your code goes here
    return 0;
}

In short: Only include header files, and never put function or variable definitions in headers (headers should only contain declarations).

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • The code structure shown is more than a tad undesirable (and self-inconsistent — it refers to `BinaryFile.c` in the code and mainly `BinaryFile.h` in the commentary, for example), but 'never' is a strong term and I'll refer you to [How to test a static function?](http://stackoverflow.com/questions/593414/how-to-test-a-static-function) for an exception to your 'never' rule. Nevertheless, 'very seldom' or 'only under special circumstances' definitely applies, and the code in the question does not qualify. Other than this, good answer. (Oh, `static inline` function definitions may go in headers.) – Jonathan Leffler May 13 '17 at 21:30