How to Implement Singly Linked List in C?

What is singly linked list?

In computer science, linked list is a collection of data elements, also called nodes, which are connected or linked by means of pointers or references. Connected or linked means if you have access to an element, you can go to the next element. In Singly linked list the links are unidirectional. You can only traverse from the first element to the last element but not in other direction.

Linked List
The above diagram shows a pictorial view of a singly linked list. This linked list has four data elements or nodes. Each node has an integer value and a link. The link either links (points) to NULL (in case of last node) or to another node, also referred as next node. The link (pointer in C) to the first node is also called head. This head is used to access the  whole linked list in a computer program. As the above diagram is of a singly linked list, every node has only one link that links or points to the next element or NULL.  No node points to the previous node. That means if we have access to a particular node, we can access to the next nodes towards the end of the list but we can never access to previous nodes.

C data structure for singly linked list

The above code snippet shows the minimum data structures required to implement a linked list in C program. C structure (struct) is the basic building block of linked list. The structure will have two parts, data part and link part. In the above example, we have a structure call node. It has two members, one integer (val) and one link (next). For the data part we can have any type of data structure including complex C structure but the link has to be a pointer of same data type of the structure. Here the type of the structure is struct node, so the link (next) is pointer of type struct node. This will help to point to the same type of node. As I mentioned earlier we need to have one pointer (head) to point the first element of the list. So we took the head variable of type struct node which will be used to access the whole list. As we don’t have any element or node to begin with, we assigned the head with NULL.

C Implementation of Linked List

This program implements a linked list by taking inputs from the user and prints the list after that. In this program we construct the list by inserting element in the from of an existing list. The insert_front() function does that. In fact we can insert the new element in various places in the existing list. The insert_front() function first allocates memory for the new element and then insert that at the from of the existing list.

The main function takes the size of the list to be created and then asks for the value of every nodes in a loop. In every iteration it calls the insert_front() function to construct the list. After the it calls print_list() function to print the constructed list.

The sample output of the program is like this.

Leave a Reply

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