I have written a C code for linked list and have various functions such as insert, delete, and traverse declared within the same file. I want to now define these functions within separate .c files, but I am unable to find the right solution. The following code is from LinkedList.h file:
struct node
{
int data;
struct node *link;
};
struct node *head; // Global variable
void insert(int x);
void insert2(int x, int n);
void traverse();
void delete(int n);
LinkedList.c file :
#include <stdio.h>
#include <stdlib.h>
#inlcude "LinkedList.h"
#inlcude "insert.c"
#inlcude "traverse.c"
#inlcude "insert.c"
#include "insert2.c"
int main(void)
from one of the .c files `include
#include <stdlib.h>
#include "LinkedList.h"
void insert(int x)
{
struct node *curr = (struct node *)malloc(sizeof(struct node));
if(head == NULL) // for empty list condition
{
I have included only the initial part of the code for the .c files.
following error is observed on compiling LinkedList.c
In file included from delete.c:3:0,
from LinkedList.c:14:
LinkedList.h:1:8: error: redefinition of ‘struct node’
struct node
^
In file included from traverse.c:3:0,
from LinkedList.c:13:
LinkedList.h:1:8: note: originally defined here
struct node
^
In file included from delete.c:3:0,
from LinkedList.c:14:
LinkedList.h:6:14: error: conflicting types for ‘head’
struct node *head; // Global variable
^
In file included from traverse.c:3:0,
from LinkedList.c:13:
LinkedList.h:6:14: note: previous declaration of ‘head’ was here
struct node *head; // Global variable
^
In file included from insert2.c:3:0,
from LinkedList.c:15:
LinkedList.h:1:8: error: redefinition of ‘struct node’
struct node
^
In file included from delete.c:3:0,
from LinkedList.c:14:
LinkedList.h:1:8: note: originally defined here
struct node
^
In file included from insert2.c:3:0,
from LinkedList.c:15:
LinkedList.h:6:14: error: conflicting types for ‘head’
struct node *head; // Global variable
^
In file included from traverse.c:3:0,
from LinkedList.c:13:
LinkedList.h:6:14: note: previous declaration of ‘head’ was here
struct node *head; // Global variable
^
In file included from insert.c:3:0,
from LinkedList.c:16:
LinkedList.h:1:8: error: redefinition of ‘struct node’
struct node
^
In file included from insert2.c:3:0,
from LinkedList.c:15:
LinkedList.h:1:8: note: originally defined here
struct node
^
In file included from insert.c:3:0,
from LinkedList.c:16:
LinkedList.h:6:14: error: conflicting types for ‘head’
struct node *head; // Global variable
^
In file included from traverse.c:3:0,
from LinkedList.c:13:
LinkedList.h:6:14: note: previous declaration of ‘head’ was here
struct node *head; // Global variable
^
In file included from LinkedList.c:17:0:
LinkedList.h:1:8: error: redefinition of ‘struct node’
struct node
^
In file included from insert.c:3:0,
from LinkedList.c:16:
LinkedList.h:1:8: note: originally defined here
struct node
^
In file included from LinkedList.c:17:0:
LinkedList.h:6:14: error: conflicting types for ‘head’
struct node *head; // Global variable
^
In file included from traverse.c:3:0,
from LinkedList.c:13:
LinkedList.h:6:14: note: previous declaration of ‘head’ was here
struct node *head; // Global variable
Following error is observed on compiling .c files wherein function is defined
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status