The Doubly Linked List class in Javascript

A doubly linked list is a data structure where each node contains references to both the next and previous nodes, allowing bidirectional traversal. Here's a complete implementation of a DoublyLinkedList class in JavaScript.

Complete Implementation

class DoublyLinkedList {
   constructor() {
      this.head = null;
      this.tail = null;
      this.length = 0;
   }
   
   insert(data, position = this.length) {
      let node = new this.Node(data);
      
      // List is currently empty
      if (this.head === null) {
         this.head = node;
         this.tail = node;
         this.length++;
         return this.head;
      }
      
      // Insertion at head
      if (position == 0) {
         node.prev = null;
         node.next = this.head;
         this.head.prev = node;
         this.head = node;
         this.length++;
         return this.head;
      }
      
      let iter = 1;
      let currNode = this.head;
      while (currNode.next != null && iter < position) {
         currNode = currNode.next; 
         iter++;
      }
      
      // Make new node point to next node in list
      node.next = currNode.next;
      
      // Make next node's previous point to new node
      if (currNode.next != null) {
         currNode.next.prev = node;
      }
      
      // Make our node point to previous node
      node.prev = currNode;

      // Make previous node's next point to new node
      currNode.next = node;

      // Check if inserted element was at the tail
      if (this.tail.next != null) {
         this.tail = this.tail.next;
      }
      
      this.length++;
      return node;
   }
   
   remove(position = 0) {
      if (this.length === 0) {
         console.log("List is already empty");
         return;
      }
      
      let currNode = this.head;
      
      if (position <= 0) {
         this.head = this.head.next;
         if (this.head) {
            this.head.prev = null;
         }
      } else if (position >= this.length - 1) {
         this.tail = this.tail.prev;
         if (this.tail) {
            this.tail.next = null;
         }
      } else {
         let iter = 0;
         while (iter < position) {
            currNode = currNode.next;
            iter++;
         }
         currNode.prev.next = currNode.next;
         currNode.next.prev = currNode.prev;
      }
      
      this.length--;
      return currNode;
   }
   
   display() {
      if (this.length === 0) {
         console.log("List is empty");
         return;
      }
      
      let result = [];
      let currNode = this.head;
      while (currNode != null) {
         result.push(currNode.data);
         currNode = currNode.next;
      }
      console.log(result.join(" <-> "));
   }
}

DoublyLinkedList.prototype.Node = class {
   constructor(data) {
      this.data = data;
      this.next = null;
      this.prev = null;
   }
};

Example Usage

// Create a new doubly linked list
let dll = new DoublyLinkedList();

// Insert elements
dll.insert(10);
dll.insert(20);
dll.insert(30);
dll.insert(15, 1); // Insert at position 1

console.log("List after insertions:");
dll.display();

console.log("List length:", dll.length);

// Remove element at position 2
dll.remove(2);
console.log("After removing element at position 2:");
dll.display();
List after insertions:
10 <-> 15 <-> 20 <-> 30
List length: 4
After removing element at position 2:
10 <-> 15 <-> 30

Key Features

The DoublyLinkedList class provides:

  • Bidirectional traversal - Can move forward and backward through nodes
  • Insert method - Adds nodes at any position (default: end of list)
  • Remove method - Removes nodes by position
  • Display method - Shows the current list structure
  • Head and tail pointers - Efficient access to both ends

Node Structure

Each node contains three properties:

  • data - The actual value stored
  • next - Reference to the next node
  • prev - Reference to the previous node

Conclusion

This DoublyLinkedList implementation provides efficient insertion and removal operations while maintaining bidirectional links. The class handles edge cases like empty lists and boundary insertions properly.

Updated on: 2026-03-15T23:18:59+05:30

546 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements