使用JavaScript實現(xiàn)一個靜態(tài)鏈表
最近重新開始翻起《大話數(shù)據(jù)結(jié)構(gòu)》,看到了靜態(tài)鏈表部分里面講C語言是利用數(shù)組模擬,覺得十分有趣。但是在JavaScript中,也可以用類似的方式去實現(xiàn),定義一個數(shù)據(jù)域和一個結(jié)點域,然后實現(xiàn)鏈表的基礎(chǔ)操作。弱類型語言沒有指針,所以需要自己區(qū)實現(xiàn)。算法的樂趣就在于解決一些思路上的問題,直擊問題的本質(zhì)。
首先可以定義Node類,如下所示:
class Node {
constructor(value) {
this.data = value;
this.next = null;
}
}然后實現(xiàn)StaticLinkedList類,先定義簡單的append和display方法:
class StaticLinkedList {
constructor() {
this.head = null;
this.length = 0;
}
append(value) {
const newNode = new Node(value);
this.length++;
if (this.head === null) {
this.head = newNode;
return;
}
let current = this.head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
display() {
console.log('the static linked list is:\r\n');
let current = this.head;
if (current === null) {
console.log('empty!');
return;
}
while (current !== null) {
console.log(JSON.stringify(current));
console.log(`its value is ${current.data}\r\n`);
current = current.next;
}
}
}其中append方法是在鏈表尾部添加新的Node對象,display方法可以打印出Node對象和它的數(shù)據(jù)。使用這個靜態(tài)鏈表類也很簡單,比如添加4個結(jié)點到這個鏈表里面:
const staticLinkedList = new StaticLinkedList(); staticLinkedList.append(3); staticLinkedList.append(7); staticLinkedList.append(16); staticLinkedList.append(24);
我們還應(yīng)該提供更加靈活添加結(jié)點的方法,比如我想在第三個結(jié)點位置插入一個新的結(jié)點,數(shù)值為11,那么現(xiàn)有的append方法就不適用了,需要定義一個新的插入結(jié)點的方法,代碼如下:
/**
* Method to insert an new element at the specific location
*
* @param {*} elementValue the value of the element that to be inserted
* @param {*} index the position of the element, from 1 to maximum of the list
* @returns true/false
*/
insertAt(elementValue, index) {
if (index < 1 || index > this.length + 1) {
console.log('index is out of the range!');
return false;
}
const newNode = new Node(elementValue);
let startPos = 1;
let current = this.head;
while (startPos < index - 1) {
current = current.next;
startPos++;
}
newNode.next = current.next;
current.next = newNode;
this.length++;
return true;
}這段代碼需要理解的是新結(jié)點如何添加到鏈表的那兩行代碼,首先是newNode.next = current.next,這行代碼是把新結(jié)點的next指向了原來插入前位置的結(jié)點的下一個結(jié)點。然后current.next = nextNode,把新結(jié)點替換掉原來該位置的結(jié)點。
為了更好地理解,我畫了一張示意圖:

要注意的是step1和step2的順序不能顛倒,否則會導(dǎo)致代碼運行錯誤。
然后我們還需要定義一個移除指定位置結(jié)點的方法,如下所示:
removeAt(index) {
if (index < 1 || index > this.length + 1) {
console.log('index is out of the range!');
return;
}
let current = this.head;
let startPos = 1;
let previous = null;
while (startPos < index) {
previous = current;
current = current.next;
startPos++;
}
previous.next = current.next;
this.length--;
}我對previous.next = current.next也畫了一張示意圖,刪除原來結(jié)點,需要把它前面一個結(jié)點的next指向該結(jié)點的next。

總結(jié):靜態(tài)鏈表的添加和移除略有不同,需要利用Nod
到此這篇關(guān)于使用JavaScript實現(xiàn)一個靜態(tài)鏈表的文章就介紹到這了,更多相關(guān)JavaScript靜態(tài)鏈表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Web?Components使用生命周期回調(diào)函數(shù)實例詳解
這篇文章主要為大家介紹了Web?Components使用生命周期回調(diào)函數(shù)實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10
超鏈接怎么正確調(diào)用javascript函數(shù)
本文介紹使用超鏈接調(diào)用javasript函數(shù)且不會影響GIF圖片動畫的方法,有遇到相同問題的小伙伴可以參考一下。2016-05-05
JS實現(xiàn)頁面跳轉(zhuǎn)參數(shù)不丟失的方法
這篇文章主要介紹了JS實現(xiàn)頁面跳轉(zhuǎn)參數(shù)不丟失的方法,結(jié)合實例形式對比分析了javascript URL加密函數(shù)escape()、encodeURI()與encodeURIComponent()的功能與相關(guān)使用技巧,需要的朋友可以參考下2016-11-11

