Open In App

How to traverse Doubly Linked List in JavaScript?

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

This is head to tail movement of the pointer in a list. The normal traversal is done as:



Example:




// 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;
        }
    }
}

Reverse traversal

This is head to tail movement of the pointer in a list. The normal traversal is done as:



Example:




// To display/ traverse list in reverse
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;
        }
    }
}

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.




// 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();
// Display the reverse list
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





Article Tags :