JS雙向鏈表實(shí)現(xiàn)與使用方法示例(增加一個(gè)previous屬性實(shí)現(xiàn))
本文實(shí)例講述了JS雙向鏈表實(shí)現(xiàn)與使用方法。分享給大家供大家參考,具體如下:
前面一篇講述了《JS基于對(duì)象的鏈表實(shí)現(xiàn)與使用方法》,這里的雙向鏈表通過(guò)增加一個(gè)previous屬性實(shí)現(xiàn)。
單鏈表中若需要查找某一個(gè)元素時(shí),必須從第一個(gè)元素開(kāi)始進(jìn)行查找,而雙向鏈表除開(kāi)頭節(jié)點(diǎn)和最后一個(gè)節(jié)點(diǎn)外每個(gè)節(jié)點(diǎn)中儲(chǔ)存有兩個(gè)指針,這連個(gè)指針?lè)謩e指向前一個(gè)節(jié)點(diǎn)的地址和后一個(gè)節(jié)點(diǎn)的地址,這樣無(wú)論通過(guò)那個(gè)節(jié)點(diǎn)都能夠?qū)ふ业狡渌墓?jié)點(diǎn)。
原理如下圖所示:
示例代碼:
/*雙向鏈表 * */ function Node(element) { this.element = element; this.next = null; this.previous = null;//雙向鏈表在這里需要增加一個(gè)previous屬性 } function LList() { this.head = new Node("head"); this.find = find; this.insert = insert; this.display = display; this.remove = remove; this.findLast = findLast; this.dispReverse = dispReverse;//將鏈表反轉(zhuǎn) } function dispReverse() { var currNode = this.head; currNode = this.findLast(); var nodestr = ""; while (!(currNode.previous == null)) { nodestr += " "+currNode.element; currNode = currNode.previous; } console.log("將鏈表反轉(zhuǎn)后: "+nodestr); } function findLast() { var currNode = this.head; while (!(currNode.next == null)) { currNode = currNode.next; } return currNode; } function remove(item) { var currNode = this.find(item); if (!(currNode.next == null)) { currNode.previous.next = currNode.next; currNode.next.previous = currNode.previous; currNode.next = null; currNode.previous = null; } } // findPrevious is no longer needed /*function findPrevious(item) { var currNode = this.head; while (!(currNode.next == null) && (currNode.next.element != item)) { currNode = currNode.next; } return currNode; }*/ function display() { var currNode = this.head; var nodestr = ""; while (!(currNode.next == null)) { nodestr += " "+currNode.next.element; currNode = currNode.next; } console.log(nodestr); } function find(item) { var currNode = this.head; while (currNode.element != item) { currNode = currNode.next; } return currNode; } function insert(newElement, item) { var newNode = new Node(newElement); var current = this.find(item); newNode.next = current.next; newNode.previous = current;//雙向鏈表在這里需要設(shè)置新節(jié)點(diǎn)previous屬性 current.next = newNode; } var cities = new LList(); cities.insert("Conway", "head"); cities.insert("Russellville", "Conway"); cities.insert("Carlisle", "Russellville"); cities.insert("Alma", "Carlisle"); cities.display();//Conway Russellville Carlisle Alma cities.remove("Carlisle"); cities.display();//Conway Russellville Alma cities.dispReverse();// Alma Russellville Conway
這里使用在線HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun測(cè)試上述代碼,可得如下運(yùn)行結(jié)果:
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》、《JavaScript排序算法總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》及《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
相關(guān)文章
js中將時(shí)間戳轉(zhuǎn)化成YYYY-MM-DD?HH:mm:ss的3種實(shí)現(xiàn)辦法
最近開(kāi)發(fā)中需要和后端進(jìn)日期和時(shí)間傳值,前后端約定為時(shí)間戳的格式,但是前端展示需要展示成年-月-日的格式,就需要進(jìn)行日期和時(shí)間轉(zhuǎn)換格式,這篇文章主要給大家介紹了關(guān)于js中將時(shí)間戳轉(zhuǎn)化成YYYY-MM-DD?HH:mm:ss的3種實(shí)現(xiàn)辦法,需要的朋友可以參考下2024-06-06js+html獲取系統(tǒng)當(dāng)前時(shí)間
這篇文章主要為大家詳細(xì)介紹了javascript html獲取系統(tǒng)當(dāng)前時(shí)間,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11

Javascript基礎(chǔ)教程之比較null和undefined值

使用getBoundingClientRect方法實(shí)現(xiàn)簡(jiǎn)潔的sticky組件的方法

基于JavaScript實(shí)現(xiàn)數(shù)碼時(shí)鐘效果

微信小程序?qū)崿F(xiàn)簡(jiǎn)易計(jì)算器功能