JavaScript 中的連結串列型別
在本文中,我們將討論 JavaScript 中各種型別的連結串列。連結串列是一種順序資料結構,用於動態儲存資料。由於這種資料結構沒有索引,因此可以根據需要新增和刪除資料。這將減少記憶體儲存的浪費。
有各種型別的連結串列。它們如下所示:
- 簡單鏈表 − 項導航僅向前。
- 雙向連結串列 − 項可以向前和向後導航。
- 迴圈連結串列 − 最後一項包含下一項的第一項的連結,第一項包含上一項的最後一項的連結。
簡單鏈表
示例 1
以下示例顯示瞭如何建立一個簡單鏈表。我們建立一個 Node 類,其中包含兩個區域性變數:data 和 next。每當呼叫此類時,建構函式都會將 data 值分配給 data 變數,並將其連結到列表中已存在的先前節點。
<!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>
雙向連結串列
雙向連結串列是一種連結串列,它以兩種方式連結其中的節點。簡單來說,雙向連結串列中的一個節點連線到其前一個節點和下一個節點。因此,項可以向前和向後導航。
示例 2
以下示例顯示瞭如何建立一個雙向連結串列並使用 JavaScript 執行其操作。我們首先建立一個包含三個部分的 Node 類:previous、value 和 next。每次呼叫此類時,都會在列表中建立一個新節點。然後,我們執行各種連結串列操作,如插入、刪除、檢查狀態和顯示。
<!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>
迴圈連結串列
迴圈連結串列可以建立為單向(簡單)連結串列或雙向連結串列。
如果是簡單鏈表,則第一個元素的地址將儲存在最後一個節點的 next 部分中,但如果是雙向連結串列,則最後一項包含指向第一項的連結作為 next,而第一項包含指向最後一項的連結作為 prev。
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP