In this tutorial, we will see all about the insert operation in a doubly linked list. In the insert operation, we will add a new node to the doubly linked list at any position.
In a Doubly Linked List, we can insert a node at the following positions:
At the beginning of the list.
At the end of the list.
At the specific position of the list.
1. Insert a node at the beginning of the list.
In the below given example we will see how to insert a new node in the beginning or front the doubly linked list.
// Java Program to insert a node at the beginning of a doubly linked list
public class InsertAtBeginningInDLL {
static Node head;
// Method to insert a node in doubly linked list
static void insertNode(int data) {
Node newNode = new Node(data);
Node last = head;
if (head == null) {
head = newNode;
return;
}
while (last.next != null) {
last = last.next;
}
last.next = newNode;
newNode.prev = last;
}
// Method to insert a node at the beginning of the list
static Node insertAtBeginning(Node head, int data) {
Node new_node = new Node(data);
new_node.next = head;
// Set previous of head as new node
if (head != null) {
head.prev = new_node;
}
return new_node;
}
// Method to print the doubly linked list
public static void printList(Node head) {
Node node = head;
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
System.out.println();
}
public static void main(String[] args) {
// Create a doubly linked list
insertNode(10);
insertNode(20);
insertNode(30);
insertNode(40);
insertNode(50);
System.out.println("Original doubly linked list:");
printList(head);
// Insert a new node at the front of the list
head = insertAtBeginning(head, 90);
// Print the updated list
System.out.print("List, After inserting a node at the beginning:\n");
printList(head);
}
}
Original doubly linked list: 10 20 30 40 50 List, After inserting a node at the beginning: 90 10 20 30 40 50
2. Insert a node at the end of the doubly linked list.
In the below given example we will see how to insert a new node at the end of the doubly linked list.
// Java Program to insert a node at the end of doubly linked list
public class InsertAtEndInDLL {
static Node head;
// Method to insert a node in doubly linked list
static void insertNode(int data) {
Node newNode = new Node(data);
Node last = head;
if (head == null) {
head = newNode;
return;
}
while (last.next != null) {
last = last.next;
}
last.next = newNode;
newNode.prev = last;
}
// Method to insert a new node at the end of the DLL
static Node insertNodeAtEnd(Node head, int newData) {
Node newNode = new Node(newData);
if (head == null) {
head = newNode;
} else {
Node node = head;
while (node.next != null) {
node = node.next;
}
// Set the next of last node to the new node
node.next = newNode;
// Set the prev of new node to the last node
newNode.prev = node;
}
return head;
}
// Method to print the doubly linked list
public static void printList(Node head) {
Node node = head;
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
System.out.println();
}
public static void main(String[] args) {
// Create a doubly linked list
insertNode(10);
insertNode(20);
insertNode(30);
insertNode(40);
insertNode(50);
System.out.println("Original doubly linked list:");
printList(head);
// Insert a new node with data 60 at the end
head = insertNodeAtEnd(head, 60);
System.out.println("After inserting, doubly linked list:");
printList(head);
}
}
Original doubly linked list: 10 20 30 40 50 After inserting, doubly linked list: 10 20 30 40 50 60
3. Insert a node at a specific position of the list
In the below given example we will see how to insert a new node in the specific position in the doubly linked list.
// Java Program to insert a node at the specific position in DLL
public class InsertAtSpecPositionInDLL {
static Node head;
// Method to insert a node in a doubly linked list
static void insertNode(int data) {
Node newNode = new Node(data);
Node last = head;
if (head == null) {
head = newNode;
return;
}
while (last.next != null) {
last = last.next;
}
last.next = newNode;
newNode.prev = last;
}
// Method to insert a node at the specific position in a doubly linked list
static Node insertNodeAtPosition(Node head, int position, int data) {
Node new_node = new Node(data);
// Insertion at the beginning
if (position == 1) {
new_node.next = head;
if (head != null) {
head.prev = new_node;
}
head = new_node;
return head;
}
Node curr = head;
for (int i = 1; i < position - 1 && curr != null; ++i) {
curr = curr.next;
}
// If the position is out of bounds
if (curr == null) {
System.out.println("Error: Given position is out of bounds.");
return head;
}
new_node.prev = curr;
new_node.next = curr.next;
curr.next = new_node;
if (new_node.next != null) {
new_node.next.prev = new_node;
}
return head;
}
// Method to print the doubly linked list
public static void printList(Node head) {
Node node = head;
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
System.out.println();
}
public static void main(String[] args) {
// Create a doubly linked list
insertNode(10);
insertNode(20);
insertNode(30);
insertNode(40);
insertNode(50);
System.out.println("Original doubly linked list:");
printList(head);
// Insert new node with data 25 at position 3
int data = 25;
int position = 3;
head = insertNodeAtPosition(head, position, data);
System.out.println("After inserting, doubly linked list:");
printList(head);
}
}
Original doubly linked list: 10 20 30 40 50 After inserting, doubly linked list: 10 20 25 30 40 50