使用JavaScript實(shí)現(xiàn)一個(gè)靜態(tài)鏈表
最近重新開始翻起《大話數(shù)據(jù)結(jié)構(gòu)》,看到了靜態(tài)鏈表部分里面講C語言是利用數(shù)組模擬,覺得十分有趣。但是在JavaScript中,也可以用類似的方式去實(shí)現(xiàn),定義一個(gè)數(shù)據(jù)域和一個(gè)結(jié)點(diǎn)域,然后實(shí)現(xiàn)鏈表的基礎(chǔ)操作。弱類型語言沒有指針,所以需要自己區(qū)實(shí)現(xiàn)。算法的樂趣就在于解決一些思路上的問題,直擊問題的本質(zhì)。
首先可以定義Node類,如下所示:
class Node { constructor(value) { this.data = value; this.next = null; } }
然后實(shí)現(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對(duì)象,display方法可以打印出Node對(duì)象和它的數(shù)據(jù)。使用這個(gè)靜態(tài)鏈表類也很簡單,比如添加4個(gè)結(jié)點(diǎn)到這個(gè)鏈表里面:
const staticLinkedList = new StaticLinkedList(); staticLinkedList.append(3); staticLinkedList.append(7); staticLinkedList.append(16); staticLinkedList.append(24);
我們還應(yīng)該提供更加靈活添加結(jié)點(diǎn)的方法,比如我想在第三個(gè)結(jié)點(diǎn)位置插入一個(gè)新的結(jié)點(diǎn),數(shù)值為11,那么現(xiàn)有的append方法就不適用了,需要定義一個(gè)新的插入結(jié)點(diǎn)的方法,代碼如下:
/** * 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é)點(diǎn)如何添加到鏈表的那兩行代碼,首先是newNode.next = current.next,這行代碼是把新結(jié)點(diǎn)的next指向了原來插入前位置的結(jié)點(diǎn)的下一個(gè)結(jié)點(diǎn)。然后current.next = nextNode,把新結(jié)點(diǎn)替換掉原來該位置的結(jié)點(diǎn)。
為了更好地理解,我畫了一張示意圖:
要注意的是step1和step2的順序不能顛倒,否則會(huì)導(dǎo)致代碼運(yùn)行錯(cuò)誤。
然后我們還需要定義一個(gè)移除指定位置結(jié)點(diǎn)的方法,如下所示:
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--; }
我對(duì)previous.next = current.next也畫了一張示意圖,刪除原來結(jié)點(diǎn),需要把它前面一個(gè)結(jié)點(diǎn)的next指向該結(jié)點(diǎn)的next。
總結(jié):靜態(tài)鏈表的添加和移除略有不同,需要利用Nod
到此這篇關(guān)于使用JavaScript實(shí)現(xiàn)一個(gè)靜態(tài)鏈表的文章就介紹到這了,更多相關(guān)JavaScript靜態(tài)鏈表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Web?Components使用生命周期回調(diào)函數(shù)實(shí)例詳解
這篇文章主要為大家介紹了Web?Components使用生命周期回調(diào)函數(shù)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10超鏈接怎么正確調(diào)用javascript函數(shù)
本文介紹使用超鏈接調(diào)用javasript函數(shù)且不會(huì)影響GIF圖片動(dòng)畫的方法,有遇到相同問題的小伙伴可以參考一下。2016-05-05js實(shí)現(xiàn)的仿新浪微博完美的時(shí)間組件升級(jí)版
本博客沒有華麗的布局,只求樸實(shí)的js的代碼,只為js代碼愛好者提供,一周大概會(huì)出1-2篇js前沿代碼的文章.只是代碼,不說技術(shù)2011-12-12JS實(shí)現(xiàn)頁面跳轉(zhuǎn)參數(shù)不丟失的方法
這篇文章主要介紹了JS實(shí)現(xiàn)頁面跳轉(zhuǎn)參數(shù)不丟失的方法,結(jié)合實(shí)例形式對(duì)比分析了javascript URL加密函數(shù)escape()、encodeURI()與encodeURIComponent()的功能與相關(guān)使用技巧,需要的朋友可以參考下2016-11-11基于JS實(shí)現(xiàn)文字轉(zhuǎn)語音即文本朗讀
這篇文章主要為大家詳細(xì)介紹了JavaScript如何利用SpeechSynthesis實(shí)現(xiàn)文字轉(zhuǎn)語音即文本朗讀的功能,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10基于JS實(shí)現(xiàn)動(dòng)態(tài)跟隨特效的示例代碼
這篇文章主要介紹了如何利用JavaScript實(shí)現(xiàn)動(dòng)態(tài)跟隨特效,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)JS有一定的幫助,感興趣的小伙伴可以了解一下2022-06-06