If the code used calloc() rather than malloc(), there would be no need to initialize next to NULL since the memory returned by
calloc() is cleared.
I would recommend separating the creation of the node from the insertion. This is more along the lines of the Single Responsibility Principle. The Single Responsibility Principle states:
that every module, class, or function should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by that module, class or function.
While the program does perform some action if the
malloc() fails, it is generally better to provide error messages to the user telling them the program failed.
Node* new_node(int data)
Node* return_value = calloc(1, sizeof(*return_value));
fprintf(stderr, "malloc of node failed in new_node()\n");
return_value->data = data;
There is a well defined set of operations on linked lists:
- Traversal : To traverse all the nodes one after another.
- Insertion : To add a node at the given position.
- Deletion : To delete a node.
- Searching : To search an element(s) by value.
- Updating : To update a node.
- Sorting: To arrange nodes in a linked list in a specific order.
- Merging: To merge two linked lists into one.
If you implement one, you should implement all. Sometimes insertion and append are implemented as separate functions.
You might want to have a specialized list pointer to point to the head that maintains more information:
typedef struct listhead
unsigned int count;
Avoid Global Variables
It is very difficult to read, write, debug and maintain programs that use global variables. Global variables can be modified by any function within the program and therefore require each function to be examined before making changes in the code. In C and C++ global variables impact the namespace and they can cause linking errors if they are defined in multiple files. The answers in this stackoverflow question provide a fuller explanation.
Each operation for the linked list should have a first parameter that is the head of the list.