Doubly linked lists in Javascript


In this article, we are going to discuss a Doubly Linked List Class data structure in JavaScript. This is a linear data structure. Doubly linked lists are almost the same as a singly linked list in all operations, we just need to keep track of one extra link per node. In singly linked lists, we just had next links, in doubly linked lists, we have 2 links, next and prev.

Doubly linked lists are represented as −

Note that in the class itself, we also need to keep track of the tail(last element).

Example

In this example, we understand the implementation of doubly linked list and its operations in JavaScript. Here, we create the list while inserting the data into it; and possibly delete nodes while removing the data from them. We are also printing the list using a user defined method print().

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8" />
      <meta http-equiv="X-UA-Compatible" content="IE=edge" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <title>Doubly Linked List Data Structure</title>
   </head>
   <body>
      <script type="text/javascript">
         function createNode(value) {
            return {
               value: value,
               next: null,
               previous: null,
            };
         }
         class DoublyLinkedList {
            constructor() {
               this.head = null;
               this.tail = null;
               this.size = 0;
            }
            insert(value) {
               this.size++;
               let newNode = createNode(value);
               if (this.tail) {
                  this.tail.next = newNode;
                  newNode.previous = this.tail;
                  this.tail = newNode;
                  return newNode;
               }
               this.head = this.tail = newNode;
               return newNode;
            }
            remove() {
               if (this.tail) {
                  this.size--;
                  let removedTail = this.tail;
                  let beforeTail = this.tail.previous;
                  this.tail = beforeTail;
                  if (this.tail) {
                     this.tail.next = null;
                  } else {
                     this.head = null;
                  }
                  return removedTail;
               }
               return undefined;
            }
            print() {
               document.write("The Elements in the Doubly Linked List are :</br> ");
               let current = this.head;
               while (current) {
                  document.write(
                     `${current.previous?.value}   ${current.value}   ${current.next?.value}`
                  );
                  current = current.next;
               }
            }
            insertHead(value) {
               this.size++;
               let newNode = createNode(value);
               if (this.head) {
                  this.head.previous = newNode;
                  newNode.next = this.head;
                  this.head = newNode;
                  return newNode;
               }
               this.head = this.tail = newNode;
               return newNode;
            }
            insertIndex(value, index) {
               if (index >= this.size) {
                  throw new Error("Insert index out of bounds");
               }
               if (index === 0) {
                  return this.insertHead(value);
               }
               this.size++;
               let currentNode = this.head;
               for (let i = 0; i < index; i++) {
                  currentNode = currentNode.next;
               }
               let previousNode = currentNode.previous;
               let newNode = createNode(value);
               newNode.next = currentNode;
               newNode.previous = previousNode;
               previousNode.next = newNode;
               currentNode.previous = newNode;
               return newNode;
            }
         }
         let dLinkedList = new DoublyLinkedList();
         dLinkedList.insert(7);
         dLinkedList.insert(8);
         dLinkedList.insert(9);
         dLinkedList.print();
      </script>
   </body>
</html>

Updated on: 16-Dec-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements