Immediate notes:
- Casting
malloc(as well asreallocandcalloc) is not necessary in C. Leaving them out will not cause warnings or errors and may suppress warnings that would otherwise be helpful. It is necessary in C++, but that's only relevant if you're using a C++ compiler to compile C code. Some reading on Stack Overflow: Should I cast the result of malloc (in C)? - You are not verifying that
mallochas succeeded before dereferencing that pointer by checking it againstNULLbefore use. This can lead to undefined behavior. Seelinkedlist_initfor an example of this. - In the same function you use the
listargument before checking that it is notNULL. You may know that in your use a null pointer is never passed in, but can you guarantee that will always be the case?
From a general design standpoint, consider whether you really want to use linked lists extensively in your code, or if you want a dynamic array, more akin to C++'s std::vector. Linked lists have O(n) access and must perform a dynamic memory allocation on each insertion. A dynamic array can have O(1) access and if designed properly, only O(log n) dynamic memory allocations, and O(1) on de-allocation.
You may wish to watch Bjarne Stroustrup: Why you should avoid Linked Lists.