How to traverse Doubly Linked List in JavaScript?
Last Updated :
11 Sep, 2023
This article will demonstrate the Doubly Linked List Traversal using JavaScript. Traversal refers to the steps or movement of the pointer to access any element of the list.
Types of Traversal in Doubly Linked List Using JavaScript
- Normal Traversal: Traversing the list from Head to Tail of the list.
- Reverse Traversal: Traversing the list from the Tail element to the Head element of the list i.e. in reverse order.
Normal traversal
This is head to tail movement of the pointer in a list. The normal traversal is done as:
- Create a pointer named curr as the current pointer.
- Assign the value of the head to the curr pointer.
- Move the curr pointer till the end and display output.
Example:
Javascript
display() {
if (! this .isEmpty()) {
let curr = this .head;
console.log( "Required list is" );
while (curr !== null ) {
console.log(curr.data);
curr = curr.next;
}
}
}
|
Reverse traversal
This is head to tail movement of the pointer in a list. The normal traversal is done as:
- Create a pointer named curr as the current pointer.
- Assign the value of the tail to the curr pointer.
- Move the curr pointer backward till start and display output.
Example:
Javascript
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;
}
}
}
|
Implementation of traversal in Doubly Linked List
Example: In this example, we will add some items in the linked list and Display the final list in both normal and reverse order by traversing in both direction.
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();
dll.displayRev();
|
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
Required list in reverse order is
27
65
25
17
29
Share your thoughts in the comments
Please Login to comment...