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.
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
struct node{ int val; struct node * next; }; struct node *head = NULL;
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
#include <stdio.h> #include <stdlib.h> struct node{ int val; struct node *next; }; void print_list(struct node *head) { printf("H->"); while(head) { printf("%d->", head->val); head = head->next; } printf("|||\n"); } void insert_front(struct node **head, int value) { struct node * new_node = NULL; new_node = (struct node *)malloc(sizeof(struct node)); if (new_node == NULL) { printf("Failed to insert element. Out of memory"); } new_node->val = value; new_node->next = *head; *head = new_node; } void main() { int count = 0, i, val; struct node * head = NULL; printf("Enter number of elements: "); scanf("%d", &count); for (i = 0; i < count; i++) { printf("Enter %dth element: ", i); scanf("%d", &val); insert_front(&head, val); } printf("Linked List: "); print_list(head); }
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.
Enter number of elements: 4 Enter 0th element: 41 Enter 1th element: 53 Enter 2th element: 56 Enter 3th element: 76 Linked List: H->76->56->53->41->|||
The post How to Implement Singly Linked List in C? appeared first on QnA Plus.