JavaScript 链表数据结构


在本文中,我们将讨论 JavaScript 中的链表数据结构。

链表是由通过链接连接在一起的数据结构的序列。

链表是由包含项的链接序列。每个链接都包含对另一个链接的连接。它是使用最广泛的数据结构之一。在创建链表时,我们将使用一些术语。

节点−这表示链表中的每个元素。它包含 2 个部分,数据和 next。数据包含我们要存储的数据,而 next 包含对列表中下一个元素的引用。

链接−每个 next 引用都是一个链接。

−对第一个元素的引用称为头。

示例

以下示例演示了 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>Linked List Data Structure</title>
   </head>
   <body>
      <script type="text/javascript">
         class Node {
            constructor(data) {
               this.element = data;
               this.next = null;
            }
         }
         class LinkedList {
            constructor() {
               this.head = null;
               this.size = 0;
            }
            add(element) {
               let node = new Node(element);
               let temp;
               if (this.head == null) this.head = node;
               else {
                  temp = this.head;
                  while (temp.next) {
                     temp = temp.next;
                  }
                  temp.next = node;
               }
               this.size++;
            }
            insertAt(element, index) {
               if (index < 0 || index > this.size)
               return document.write("Enter a valid index.");
               else {
                  let node = new Node(element);
                  let curr, prev;
                  curr = this.head;
                  if (index == 0) {
                     node.next = this.head;
                     this.head = node;
                  } else {
                     curr = this.head;
                     let it = 0;
                     while (it < index) {
                        it++;
                        prev = curr;
                        curr = curr.next;
                     }
                     node.next = curr;
                     prev.next = node;
                  }
                  this.size++;
               }
            }
            removeFrom(index) {
               if (index < 0 || index >= this.size)
               return document.write("Enter a valid index");
               else {
                  let curr,
                  prev,
                  it = 0;
                  curr = this.head;
                  prev = curr;
                  if (index === 0) {
                     this.head = curr.next;
                  } else {
                     while (it < index) {
                        it++;
                        prev = curr;
                        curr = curr.next;
                     }
                     prev.next = curr.next;
                  }
                  this.size--;
                  return curr.element;
               }
            }
            removeElement(element) {
               let curr = this.head;
               let prev = null;
               while (curr != null) {
                  if (curr.element === element) {
                     if (prev == null) {
                        this.head = curr.next;
                     } else {
                        prev.next = curr.next;
                     }
                     this.size--;
                     return curr.element;
                  }
                  prev = curr;
                  curr = curr.next;
               }
               return -1;
            }
            indexOf(element) {
               let count = 0;
               let temp = this.head;
               while (temp != null) {
                  if (temp.element === element) {
                     document.write("</br>The Element is found at the index : ");
                     return count;
                  }
                  count++;
                  temp = temp.next;
               }
               return -1;
            }
            isEmpty() {
               return this.size == 0;
            }
            size_of_list() {
               document.write("</br>The size of the Linked List is : " + this.size);
            }
            displayList() {
               let curr = this.head;
               let str = "";
               while (curr) {
                  str += curr.element + " ";
                  curr = curr.next;
               }
               document.write("The Elements in the Linked List are: " + str);
            }
         }
         let ll = new LinkedList();
         ll.add(10);
         ll.add(20);
         ll.add(30);
         ll.add(40);
         ll.add(50);
         ll.displayList();
         ll.size_of_list();
         document.write(ll.indexOf(40));
      </script>
   </body>
</html>

更新于:2022 年 12 月 16 日

630 次浏览

职业开跑

完成课程后获得认证

开始入门
广告