This article demonstrates the insert operation in a Doubly Linked List using JavaScript. The insertion in the Doubly linked list follows the given Steps.
Steps to Insert Element in Doubly Linked List
- Create a new Node element with the input data/ value.
-
Link the new node previous to the current element and next to the current’s next node.
- temp.prev => current
- temp.next => current.next
-
Link the current’s next node to the new node
- current.next.prev => temp
- current.next = > temp
Types of Insertions in the DoublyLinkedList
- Insert at the start of the list.
- Insert at the end of the list.
- Insert at any other positions or index.
Method to insert an element at the start
In this method, we will create a new data node, link it before the head pointer, and shift the head pointer back to the new first element.
Code Snippet:
Javascript
insertStart(val) { // Create a temporary variable
let temp = new Node(val);
// If the list is empty link assign
// new node to both head and tail
if ( this .head == null ) {
this .head = temp;
this .tail = temp;
}
// else add item to the head and shift head backward
else {
temp.next = this .head;
this .head.prev = temp;
this .head = temp;
}
} |
Method to insert an element at the end
In this method, we will create new node and link it with the tail pointer of the list.
Code Snippet:
Javascript
insertEnd(val) { // Create a temporary variable
let temp = new Node(val);
// If the list is empty link assign
// new node to both head and tail
if ( this .head == null ) {
this .head = temp;
this .tail = temp;
}
// else add item to the tail and shift tail
else {
temp.prev = this .tail;
this .tail.next = temp;
this .tail = this .tail.next;
}
} |
Method to insert an element at other position
To insert an element at sepcific position we will first traverse the list till the element before the positon and insert element after it.
Code Snippet:
Javascript
// method to insert value at given index/position insertAt(val, pos) { // If the index is 0 use insertStart
// method to insert value as 0 position
if (pos == 0) return this .insertStart(val);
let curr = this .head;
// Iterate to the element present
// just before given index
while (pos > 1) {
pos -= 1;
// If list do not contain enough elements
if (curr === null )
return console.log(
"Incorrect Position! Index does not exist." ,
);
// Shift the poiter at every iteration
curr = curr.next;
}
// After reaching required index create new node
let temp = new Node(val);
// Insert node at the required position
temp.next = curr.next;
temp.prev = curr;
curr.next.prev = temp;
curr.next = temp;
} |
Implementation of Insert Operations in Doubly Linked List
Example:
Javascript
// Doubly Linked list Node class Node { // Constructor to create a new node
// next and prev is by default initialized as null
constructor(val) {
// To store the value
this .data = val;
// To link the next Node
this .next = null ;
// TO link the previous Node
this .prev = null ;
}
} // Doubly Linked List class DoublyLinkedList { // Constructor to create a new linked list
constructor() {
// To contain the first item of the list
this .head = null ;
// To contain the last item of the list
this .tail = null ;
}
// To check if the list is empty
isEmpty() {
if ( this .head == null ) return true ;
return false ;
}
// Method to add item at the last of doubly linked list
insertEnd(val) {
// Create a temporary variable
let temp = new Node(val);
// If the list is empty link assign
// new node to both head and tail
if ( this .head == null ) {
this .head = temp;
this .tail = temp;
}
// else add item to the tail and shift tail
else {
temp.prev = this .tail;
this .tail.next = temp;
this .tail = this .tail.next;
}
console.log( 'inserted at end' ,val)
}
insertStart(val) {
// Create a temporary variable
let temp = new Node(val);
// If the list is empty link assign
// new node to both head and tail
if ( this .head == null ) {
this .head = temp;
this .tail = temp;
}
// else add item to the head and shift head backward
else {
temp.next = this .head;
this .head.prev = temp;
this .head = temp;
}
console.log( 'inserted at start' ,val)
}
// method to insert value at given index
insertAt(val, pos) {
// If the index is 0 use insertStart
// method to insert value as 0 position
if (pos == 0) return this .insertStart(val);
let index = pos;
let curr = this .head;
// Iterate to the element present
// just before given index
while (pos > 1) {
pos -= 1;
// If list do not contain enough elements
if (curr === null )
return console.log(
"Incorrect Position! Index does not exist." ,
pos,
curr.data
);
// Shift the poiter at every iteration
curr = curr.next;
}
// After reaching required index create new node
let temp = new Node(val);
// Insert node at the required position
temp.next = curr.next;
temp.prev = curr;
curr.next.prev = temp;
curr.next = temp;
console.log( 'inserted at index' ,index, 'value' ,val)
}
// To traverse and display the list
display() {
// Check if the List is empty
if (! this .isEmpty()) {
// traverse the list using new current pointer
let curr = this .head;
console.log( 'Required list is' )
while (curr !== null ) {
// Display element
console.log(curr.data);
// Shift the current pointer
curr = curr.next;
}
}
}
displayRev() {
// Check if the List is empty
if (! this .isEmpty()) {
// traverse the list using new current pointer
let curr = this .tail;
console.log( 'Required list in reverse order is' )
while (curr !== null ) {
// Display element
console.log(curr.data);
// Shift the current pointer
curr = curr.prev;
}
}
}
} // Create new Doubly Linked List const dll = new DoublyLinkedList();
// Add elements in the list dll.insertEnd(25); dll.insertEnd(27); dll.insertStart(17); dll.insertStart(29); dll.insertAt(65,3); // Display the list dll.display(); |
Output
inserted at end 25 inserted at end 27 inserted at start 17 inserted at start 29 inserted at index 3 value 65 Required list is 29 17 25 65 27