Insertion to Linked List – Explanation, Types and Implementation

Insertion to linked list can be done in three ways:

  1. Inserting a node at the front of linked list.
  2. Inserting a node at the end of linked list.
  3. Inserting a node at a specified location of linked list.

Read more – Introduction to Linked List – Explanation and Implementation

Say we have a linked list containing the elements 10, 20 and 30. Need to insert an element 50.

Linked List
Linked list

1. Inserting a node at the Front of Linked List

As the name implied, inserting the new element 50 before the element 10. A new node will be created containing element 50 pointing to the first node above list. Head will now point to this new node.

Insertion to front
Node insertion to Front

Implementation in C

#include<stdio.h>
#include<stdlib.h>

//DECLARATION
struct node 
{
	int data;
	struct node *next;
};

// This function insert a new node to the front of linked list
struct node* insertToFront(struct node *head, int data)
{
	struct node *temp = (struct node*)malloc(sizeof(struct node));
	temp->data = data;
	temp->next = head;
	head = temp;
	return head;
}

void printLinkedList(struct node *list)
{
    while (list)
    {
    	printf(" %d ", list->data);
    	list = list->next; // accessing and assigning the next element of list
    }
}

// Main method to initialize the program
int main()
{
	//INITIALIZATION
    //Declare and allocate 3 nodes in the heap 
    struct node* head;
    struct node* first = (struct node*)malloc(sizeof(struct node)); 
    struct node* second = (struct node*)malloc(sizeof(struct node));
    struct node* third = (struct node*)malloc(sizeof(struct node));
    
	//ASSIGNINMENT
    first->data = 10; //assign data in first node
    first->next = second; // Link first node with second 
    
    second->data = 20; //assign data to second node
    second->next = third; 
    
    third->data = 30; //assign data to third node
    third->next = NULL;

    head = first;    //head pointing to the linked list
    head = insertToFront(head, 50);
    printLinkedList(head);
    
    return 0;
}
Output:-

50 10 20 30

 2. Insertion a node at the End of Linked List

As the name implied, inserting the new element 50 after the 30.  A new node will be created containing element 50 which points null and last node of the list point to the new node.

Insertion to end
Node insertion to end

 

Implementation in C

#include<stdio.h>
#include<stdlib.h>

//DECLARATION
struct node 
{
	int data;
	struct node *next;
};

// This function insert a new node to the end of linked list
void insertToEnd(struct node *list, int data)
{
	struct node *temp = (struct node*)malloc(sizeof(struct node));
	temp->data = data;
	temp->next = NULL;
	while(list->next){
		list = list->next;
		}
	list->next = temp;
}

void printLinkedList(struct node *list)
{
    while (list)
    {
    	printf(" %d ", list->data);
    	list = list->next; // accessing and assigning the next element of list
    }
}

// Main method to initialize the program
int main()
{
	//INITIALIZATION
    //Declare and allocate 3 nodes in the heap 
    struct node* head;
    struct node* first = (struct node*)malloc(sizeof(struct node)); 
    struct node* second = (struct node*)malloc(sizeof(struct node));
    struct node* third = (struct node*)malloc(sizeof(struct node));
    
	//ASSIGNINMENT
    first->data = 10; //assign data in first node
    first->next = second; // Link first node with second 
    
    second->data = 20; //assign data to second node
    second->next = third; 
    
    third->data = 30; //assign data to third node
    third->next = NULL;

    head = first;     //head pointing to the linked list
    insertToEnd(head, 50);
    printLinkedList(head);
    
    return 0;
}
Output:-

 10 20 30 50

3. Insertion a node at the Specified location in linked list

Say want to insert element 50 after element 20. A new node containing 50 will be created and Node containing 20 will point New node and that new node will point the node containing 30.

Insertion as specified
Node insertion after element 20

Implementation in C

#include<stdio.h>
#include<stdlib.h>

//DECLARATION
struct node 
{
	int data;
	struct node *next;
};

// This function insert a new node after 20
void insertToPosition(struct node *list, int knownData, int data)
{
	struct node *temp = (struct node*)malloc(sizeof(struct node));
	temp->data = data;

	while(list->data != 20){
		list = list->next;
		}
	temp->next = list->next;
	list->next = temp;
}

void printLinkedList(struct node *list)
{
    while (list)
    {
    	printf(" %d ", list->data);
    	list = list->next; // accessing and assigning the next element of list
    }
}

// Main method to initialize the program
int main()
{
	//INITIALIZATION
    //Declare and allocate 3 nodes in the heap 
    struct node* head;
    struct node* first = (struct node*)malloc(sizeof(struct node)); 
    struct node* second = (struct node*)malloc(sizeof(struct node));
    struct node* third = (struct node*)malloc(sizeof(struct node));
    
	//ASSIGNINMENT
    first->data = 10; //assign data in first node
    first->next = second; // Link first node with second 
    
    second->data = 20; //assign data to second node
    second->next = third; 
    
    third->data = 30; //assign data to third node
    third->next = NULL;

    head = first;   //head pointing to the linked list
    
    //Insert after 20
    insertToPosition(head, 20, 50);
    printLinkedList(head);
    
    return 0;
}

Output:-

 10 20 50 30

Note:-
There is one more variant of insertion a node a specified location in which instead of value, a location is given where we have to insert the new node. For ex – insert a node at 3rd position from the beginning, insert element as the second last element of the list etc. Here element is not mentioned rather position is provided. In such scenarios instead of checking condition on the values, we keep a track on the number of nodes processed and then insert the node at the specified location.

Knowledge is most useful when liberated and shared. Share this to motivate us to keep writing such online tutorials for free and do comment if anything is missing or wrong or you need any kind of help.

Keep Learning… Happy Learning.. 🙂

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index