JavaScript 中的双向链表


在本文中,我们将讨论 JavaScript 中的双向链表类数据结构。 这是一种线性数据结构。 双向链表在所有操作中几乎与单向链表相同,我们只需要每节点额外跟踪一个链接。 在单向链表中,我们只有下一个链接,在双向链表中,我们有 2 个链接,下一个和上一个。

双向链表表示为 −

请注意,在类本身中,我们还需要跟踪尾部(最后一个元素)。

示例

在此示例中,我们了解双向链表及其在 JavaScript 中的操作。 在这里,我们在向其中插入数据时创建列表; 在从中删除数据时可能删除节点。 我们还使用用户定义的方法打印() 打印列表。

<!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>

更新于: 16-12-2022

2 千次浏览

开启您的职业生涯

完成课程即可获得认证

立即开始
广告
© . All rights reserved.