A simple linked list implementation in C – source code

From the wikipedia:

“A linked list is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of a datum and a reference (in other words, a link) to the next node in the sequence; more complex variants add additional links. This structure allows for efficient insertion or removal of elements from any position in the sequence.

Linked lists are among the simplest and most common data structures. They can be used to implement several other common abstract data types, including stacks, queues, associative arrays, and symbolic expressions, though it is not uncommon to implement the other data structures directly without using a list as the basis of implementation.”

For beginners, linked lists are often a very high wall to climb, In this article I’m going to code the most important functions to handle linked lists and then I’ll write a simple program which uses these functions.

Essentially to handle a linked list we have to write at least 3 functions:

  • A function which adds an item to the list
  • A function which removes an item from the list.
  • A function which scans the list.

First of all we must define the structure

Then we have to insert our functions, let’s start with the addItem function, this function will add an item after the last item of the linked list.

the removeItem function will check for an item into the list, if the item is present the function will remove it from the linked list

the printListItems function will print the content of the linked list starting from the fisrt item.

Now let’s write a simple main program to test the functions we have implemented.


Put all the code into a file named linked_list.c, open a terminal window and try to compile it with the following command:

Run the program issueing the following command:

As said at the beginning of this article linked lists can be used to implement abstract data types, in the next articles we are going to implement a stack (a structure managed with a  Last In First Out policy) and a queue (a structure managed with a  First In First Out policy).


Related Posts

3 thoughts on “A simple linked list implementation in C – source code

  1. Hi,

    thanks for your functions, but you have copied the “void addItem(linked_list_t **aLinkedList, int aData)” instead of “void removeItem(linked_list_t *aList, int aData);” as code for the removeItem function.

    Regards, Holger

Leave a Reply

Your email address will not be published. Required fields are marked *