Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C program to insert a node at any position using double linked list
A doubly linked list is a linear data structure where each node contains data and two pointers − one pointing to the previous node and another to the next node. This allows traversal in both directions and efficient insertion/deletion operations at any position.
Syntax
struct node {
int data;
struct node *prev;
struct node *next;
};
void insertAtPosition(int data, int position);
Doubly Linked List Structure
The doubly linked list structure consists of nodes where each node has three components −
- Data − stores the actual value
- Previous pointer − points to the previous node
- Next pointer − points to the next node
Example: Insert Node at Any Position
The following program demonstrates how to insert a node at any specified position in a doubly linked list −
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *prev;
struct node *next;
} *head = NULL;
void createList(int n) {
struct node *newNode, *temp;
int data;
for(int i = 0; i < n; i++) {
printf("Enter data for node %d: ", i + 1);
scanf("%d", &data);
newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
if(head == NULL) {
head = newNode;
} else {
temp = head;
while(temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
}
}
void insertAtPosition(int data, int position) {
struct node *newNode, *temp;
int count = 1;
newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
if(position == 1) {
newNode->next = head;
newNode->prev = NULL;
if(head != NULL) {
head->prev = newNode;
}
head = newNode;
return;
}
temp = head;
while(temp != NULL && count < position - 1) {
temp = temp->next;
count++;
}
if(temp == NULL) {
printf("Invalid position<br>");
free(newNode);
return;
}
newNode->next = temp->next;
newNode->prev = temp;
if(temp->next != NULL) {
temp->next->prev = newNode;
}
temp->next = newNode;
}
void displayList() {
struct node *temp = head;
int count = 1;
printf("\nDoubly Linked List: ");
while(temp != NULL) {
printf("Node %d: %d ", count++, temp->data);
temp = temp->next;
}
printf("<br>");
}
int main() {
int n, data, position;
printf("Enter number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nOriginal list:");
displayList();
printf("\nEnter position to insert new node: ");
scanf("%d", &position);
printf("Enter data for new node: ");
scanf("%d", &data);
insertAtPosition(data, position);
printf("\nAfter insertion:");
displayList();
return 0;
}
Enter number of nodes: 3 Enter data for node 1: 10 Enter data for node 2: 20 Enter data for node 3: 30 Original list: Doubly Linked List: Node 1: 10 Node 2: 20 Node 3: 30 Enter position to insert new node: 2 Enter data for new node: 15 After insertion: Doubly Linked List: Node 1: 10 Node 2: 15 Node 3: 20 Node 4: 30
Key Points
- Position-based insertion requires traversing to the correct position
- Memory allocation check is essential to avoid runtime errors
- Pointer updates must maintain both forward and backward links
- Edge cases include insertion at beginning, end, and invalid positions
Conclusion
Inserting a node at any position in a doubly linked list involves careful pointer manipulation to maintain both forward and backward links. The operation has O(n) time complexity for position-based access but provides flexibility for dynamic data management.
