如何在 JavaScript 中刪除連結串列?
在本文中,我們將探討連結串列以及如何在 JavaScript 中刪除連結串列。
連結串列是一種用於儲存原始資料的資料結構。連結串列元素不儲存在連續的記憶體位置中。連結串列中的元素使用指標連結。
示例
在下面的示例中,我們將刪除 JavaScript 中的連結串列。
# index.html
<html>
<head>
<title>Computed Property</title>
</head>
<body>
<h1 style="color: red;">
Welcome To Tutorials Point
</h1>
<script>
// Javascript program to delete
// a linked list
// Declaring the HEAD
var head;
class Node
{
constructor(val)
{
this.data = val;
this.next = null;
}
}
// Deleting the entire linked list
function deleteList()
{
head = null;
}
// Inserting a new node.
function push(new_data)
{
/* 1 & 2: Allocate the Node &
Put in the data */
var new_node = new Node(new_data);
// 3. Make next of new Node as head
new_node.next = head;
// 4. Move the head to point to new Node
head = new_node;
}
function display() {
if(head==null) {
document.write("null");
}
while(head!=null) {
document.write("<br\>" + head.data);
head = head.next;
}
}
// Use push() to construct list
// 1->12->1->4->1
push(1);
push(4);
push(1);
push(12);
push(1);
document.write("<h3>Elements in List Before Deletion: </h3>");
display();
document.write("<br\><h4>Deleting the list</h4>");
deleteList();
document.write("<br\><h3>Elements in List After Deletion: </h3>");
display();
</script>
</body>
</html>輸出
它將產生以下輸出。

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP