Linked List Data Structure Implementation In C Language
Linked list is a data structure, a collection of data that each node in the list has reference to the next node and in a double linked list it can have a previous node. Linked lists are preferred to be used because of the efficiency in the insertion and deletion. Linked lists also can be used to implement other abstract data types like stacks, queues, etc.
The basic idea of linked list is that it has a head Node that we can iterate the next node until we find the very last node or the tail node to find specific data, usually given an index or the position of the data.
In this post I created a simple reusable implementation of singly linked list in C, which probably is not very efficient in terms of speed and performance, but for me I feel like it’s much more human readable.
This implementation of linked list has features that I will describe in a quick explanation below.
To initialize the first node, or the head node if the linked list is empty or had been cleared
To check the size of the list, will return the number of how many nodes are there that is in the list
To get specific node given an index of the list
To delete specific node given a valid index on the list
Verify whether a given index is a valid index on the list, for example a list that contains only 3 nodes will return error if the index is example given 3, 4 or more
Add new node to specific index location
Iterate through the list and display the data
Add a new data or node to the very last of the list
Add a new data or node to the very beginning of the list
Will remove the tail node of the list
Will remove the head node of the list, and then replace the head node with the second node
Very self describe, which is to clear the linked list out of the memory
If you are interested in exploring the single linked list that we made, you can clone the project from this github repository below. Here’s a link to the project linked list github repo I described above.
git clone https://github.com/mudiadamz/c_linked_list.git
This is just a very simple implementation of linked list data structure but yet it’s very interesting to learn about data structure implemented in the C language. The standard library of C language is very simple, so almost anything we need to make yourself a feature you are gonna work with. It has an array but the drawback with an array is that you need to specify the maximum number of items that could be on the array, so it wasn’t very dynamic.