Where do you include <stdlib.h> - because malloc() is declared here?
Is it a compilation problem ( malloc() undeclared) or a binding problem ( malloc() undefined)?
What is an error message?
Now we read the code:
<cstdlib> is the C ++ header (e.g. <cstdio> ).- You need to include
<stdlib.h> in C. - You need to include
<stdlib.h> where the malloc() function is used - in linkedlist.c .
You also have header protectors in the wrong place in linkedlist.h . Canonical structure for the header file:
#ifndef HEADER_H_INCLUDED #define HEADER_H_INCLUDED ...the other material in the header... ...definitions and declarations... #endif
#endif is the last non-comment, not an empty line in the file, not the third.
Your data structures are unusually complex even for a doubly linked list. Are you sure you will need the length so often that this ensures that a pointer to the head is stored in each node in the list? I would be surprised if you use it often. I assume that you have a pointer to the head in each node, so that when you remove an arbitrary node from the list, you can reduce the length of the list. You will probably be better off passing a pointer to a list, and the pointer to a node will be deleted than yours.
I see no excuse for length = 5 in the new_list() function.
In addition, C and C ++ are radically different in meaning:
struct linked_list * new_list();
In C ++, this means that new_list() is a function that takes no arguments and returns a struct linked_list .
In C, this means that " new_list() is a function with a completely undefined argument list that returns a struct linked_list pointer." The function is declared, but the function prototype is missing.
In C you should write:
struct linked_list * new_list(void);
Personally, I prefer to see extern before function declarations in headers; it is not actually needed for functions, but since it ( extern ) is (or should be) necessary for variables declared in headers, I prefer symmetry for functions declared in headers.