Insertion In Doubly Linked List In C
Insertion in a Doubly Linked List (DLL) refers to adding a new node to the list. Inserting a node in the Doubly Linked List contains very simple steps in C. A Doubly Linked List (DLL) is a type of linked list where each node includes three components:
- Data – to store the value.
- Pointer to the next node (
next
) – links to the next node in the list. - Pointer to the previous node (
prev
) – links to the previous node in the list.
There are different types of insertions based on where the new node is added in the list:
- Insertion at the Beginning
- Insertion at the End
- Insertion After a Given Node
- Insertion Before a Given Node
Requirement For Insertion in Doubly Linked List
Before proceeding for the types of insertion in DLL, consider the following requirements for insertion in a doubly linked list
01: Consider a Node Structure
struct Node { int data; struct Node* next; struct Node* prev; }; |
data
: Holds the value of the node.next
: Pointer to the next node in the list.prev
: Pointer to the previous node in the list. This allows us to traverse both forward and backward in the doubly linked list.
02: Consider a Doubly Linked List
Consider three nodes DLL where Head pointing to first node and it is also present somewhere in main memory.
- First Node (Head Node)
- Data:
10
- Next Pointer:
900
(Address of the second node) - Previous Pointer:
NULL
- Data:
- Second Node
- Data:
20
- Next Pointer:
1100
(Address of the third node) - Previous Pointer:
350
(Address of the first node)
- Data:
- Third Node
- Data:
30
- Next Pointer:
NULL
- Previous Pointer:
900
(Address of the second node)
- Data:

03: Create a New Node for Insertion
The createNode
function dynamically creates a new node and initializes its values
struct Node* createNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); if (!newNode) { printf(“Memory allocation failed\n”); exit(1); } newNode->data = data; newNode->next = NULL; newNode->prev = NULL; return newNode; } |
- A new node is allocated in memory using
malloc
. - The
data
is initialized with the input value. - Both
next
andprev
pointers are set toNULL
since the node is initially isolated.
Type 01: Insertion at Beginning in DLL
Step 01: ptr2->next = Head;
The statement (ptr2->next = head;)
updates the next
pointer of the new node (ptr2
) to point to the current head
of the linked list.
Step 02: Head->prev = ptr2;
The statement head->prev = ptr2
updates the prev
pointer of the current head node to point to the new node (ptr2
) being inserted at the beginning of the doubly linked list.
Step 03: Head= ptr2;
- After the assignment
Head = ptr2;
, bothHead
andptr2
point to the same memory location (the node). Any changes made to the node throughHead
orptr2
will affect the same node since they share the same memory address.
C Program: Insertion at the beginning in DLL
#include <stdio.h> #include <stdlib.h>// Define the structure of a node for a doubly linked list struct Node { int data; struct Node* next; struct Node* prev; };// Function to create a new node struct Node* createNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); if (!newNode) { printf(“Memory allocation failed\n”); exit(1); } newNode->data = data; newNode->next = NULL; newNode->prev = NULL; return newNode; }// Function to insert a node at the beginning of the doubly linked list struct Node* insertAtBeginning(struct Node* head, int data) { struct Node* newNode = createNode(data); // Point the new node’s next to the current head // Update the previous pointer of the current head (if it exists) // The new node becomes the new head // Function to display the doubly linked list // Main function printf(“Initial doubly linked list: “); // Insert a new node with value 90 at the beginning printf(“\nInserting %d at the beginning…\n”, newData); printf(“Doubly linked list after insertion: “); return 0; |
Output:
Initial doubly linked list: 10 <-> 20 <-> 30 <-> 40 <-> NULL Inserting 90 at the beginning... Doubly linked list after insertion: 90 <-> 10 <-> 20 <-> 30 <-> 40 <-> NULL
Type 02: Insertion at End in DLL
step 01: (ptr = head;)
ptr
is a temporary pointer used to navigate through the nodes of the linked list without modifying the head
pointer. The head
pointer always points to the first node of the list. Modifying head
would lose track of the beginning of the list.
The line ptr = head;
ensures that:
- Traversal begins at the start of the list.
- The
head
pointer remains unchanged.
step 02: ptr traversal Condition
while (ptr->next != NULL) { ptr = ptr->next; } |
This loop is used to traverse the linked list from the current node (ptr
) until it reaches the last node in the list. The last node is identified by the condition ptr->next == NULL
.
step 03: ptr -> next = ptr2
As the condition is false in step 02, statement (ptr -> next = ptr2) is executed. By setting ptr->next = ptr2
, the next
pointer of the last node now points to the new node (ptr2
).
step 04: (ptr2 -> prev = ptr;)
The line ptr2->prev = ptr
ensures that the new node (ptr2
) is properly backward-linked to the previous last node (ptr
), completing the bidirectional linkage necessary in a doubly linked list.
C Program: Insertion at the End in Doubly Linked List
#include <stdio.h> #include <stdlib.h>// Define the structure of a node for a doubly linked list struct Node { int data; struct Node* next; struct Node* prev; }; // Function to create a new node // Function to insert a node at the end of the doubly linked list // If the list is empty, the new node becomes the head // Traverse to the last node // Update pointers to insert the new node at the end return head; // Head remains unchanged // Function to display the doubly linked list // Main function printf(“Initial doubly linked list: “); // Insert a new node with value 100 at the end printf(“Doubly linked list after insertion at the end: “); return 0; |
Output
Initial doubly linked list: 10 <-> 20 <-> 30 <-> 40 <-> NULL Inserting 90 at the end... Doubly linked list after insertion at the end: 10 <-> 20 <-> 30 <-> 40 <-> 90 <-> NULL