Insert Operation in Doubly Linked List using JavaScript
Last Updated :
11 Sep, 2023
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) {
let temp = new Node(val);
if ( this .head == null ) {
this .head = temp;
this .tail = temp;
}
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) {
let temp = new Node(val);
if ( this .head == null ) {
this .head = temp;
this .tail = temp;
}
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
insertAt(val, pos) {
if (pos == 0) return this .insertStart(val);
let curr = this .head;
while (pos > 1) {
pos -= 1;
if (curr === null )
return console.log(
"Incorrect Position! Index does not exist." ,
);
curr = curr.next;
}
let temp = new Node(val);
temp.next = curr.next;
temp.prev = curr;
curr.next.prev = temp;
curr.next = temp;
}
|
Implementation of Insert Operations in Doubly Linked List
Example:
Javascript
class Node {
constructor(val) {
this .data = val;
this .next = null ;
this .prev = null ;
}
}
class DoublyLinkedList {
constructor() {
this .head = null ;
this .tail = null ;
}
isEmpty() {
if ( this .head == null ) return true ;
return false ;
}
insertEnd(val) {
let temp = new Node(val);
if ( this .head == null ) {
this .head = temp;
this .tail = temp;
}
else {
temp.prev = this .tail;
this .tail.next = temp;
this .tail = this .tail.next;
}
console.log( 'inserted at end' ,val)
}
insertStart(val) {
let temp = new Node(val);
if ( this .head == null ) {
this .head = temp;
this .tail = temp;
}
else {
temp.next = this .head;
this .head.prev = temp;
this .head = temp;
}
console.log( 'inserted at start' ,val)
}
insertAt(val, pos) {
if (pos == 0) return this .insertStart(val);
let index = pos;
let curr = this .head;
while (pos > 1) {
pos -= 1;
if (curr === null )
return console.log(
"Incorrect Position! Index does not exist." ,
pos,
curr.data
);
curr = curr.next;
}
let temp = new Node(val);
temp.next = curr.next;
temp.prev = curr;
curr.next.prev = temp;
curr.next = temp;
console.log( 'inserted at index' ,index, 'value' ,val)
}
display() {
if (! this .isEmpty()) {
let curr = this .head;
console.log( 'Required list is' )
while (curr !== null ) {
console.log(curr.data);
curr = curr.next;
}
}
}
displayRev() {
if (! this .isEmpty()) {
let curr = this .tail;
console.log( 'Required list in reverse order is' )
while (curr !== null ) {
console.log(curr.data);
curr = curr.prev;
}
}
}
}
const dll = new DoublyLinkedList();
dll.insertEnd(25);
dll.insertEnd(27);
dll.insertStart(17);
dll.insertStart(29);
dll.insertAt(65,3);
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
Share your thoughts in the comments
Please Login to comment...