摘要:一般我們都構(gòu)造雙向循環(huán)鏈表。循環(huán)鏈表的操作和單鏈表的操作基本一致,差別僅僅在于算法中的循環(huán)條件有所不同。單向循環(huán)鏈表雙向循環(huán)鏈表單向循環(huán)鏈表是在單鏈表基礎(chǔ)上,將最后一個(gè)節(jié)點(diǎn)的指針指向鏈表頭。
維基百科
雙向鏈表也叫雙鏈表,是鏈表的一種,它的每個(gè)數(shù)據(jù)結(jié)點(diǎn)中都有兩個(gè)指針,分別指向直接后繼和直接前驅(qū)。所以,從雙向鏈表中的任意一個(gè)結(jié)點(diǎn)開始,都可以很方便地訪問它的前驅(qū)結(jié)點(diǎn)和后繼結(jié)點(diǎn)。一般我們都構(gòu)造雙向循環(huán)鏈表。
又上面可知,雙向鏈表與單鏈表的區(qū)別在于,雙向鏈表的每個(gè)節(jié)點(diǎn)都有一個(gè)指向前一個(gè)的指針(previous)。
想了解單鏈表,可以看看我上一遍寫的單鏈表。
1.先創(chuàng)建節(jié)點(diǎn)的類。
class Node { constructor (element) { this.elememt = element; this.previous = null; // 這個(gè)是指向前一個(gè)的指針。 this.next = null; } }
2.再創(chuàng)建雙向鏈表的類。
class DoubleLinkedList { constructor () { this.head = null; this.length = 0; } }
接下來給雙向聯(lián)表添加一些方法(一下方法都是在雙向鏈表類里面寫的)。
Append
/** * * @param element 用來實(shí)例節(jié)點(diǎn)的數(shù)據(jù),什么數(shù)據(jù)類型都行. */ append (element) { let node = new Node(element), current; if (!this.length) { // 長度為0,則是新的鏈表。 this.head = node; // 鏈表的頭指向新的node。 } else { current = this.head; // current獲得指向第一個(gè)節(jié)點(diǎn)的指針。 while (current.next) { // 當(dāng)前node.next存在。 current = current.next; // 如果當(dāng)前節(jié)點(diǎn)(current)的next不為null,那么current.next這個(gè)指針就給了current。 } // current的下一個(gè)為null的時(shí)候,退出循環(huán)。 current.next = node; // current.next為null退出循環(huán)(即已到了最后一個(gè)節(jié)點(diǎn)),那么它的next為node node.previous = current; // 新加入的node的前一個(gè)就是current。 } this.length++; // 長度加一,別忘咯。 console.log("Append successfully!"); }
InsertNode
/** * @param element * @param {Number} position 要插入的某個(gè)位置. */ insertNode (element, position) { if (position >= 0 && position <= this.length) { let node = new Node(element), front, index = 0, current = this.head; if (!position) { // 如果插入的位置為 0 。 this.head = node; node.next = current; // node的后一個(gè)是之前head所指的,即是current。 } else { while (index++ < position) { front = current; // 當(dāng)前變?yōu)榍耙粋€(gè)。 current = current.next; // 下一個(gè)就變?yōu)楫?dāng)前 } front.next = current.previous = node; // 前一個(gè)的next 和 當(dāng)前的previous 是node。 node.previous = front; // 插入的node的前一個(gè)為front。 node.next = current; // 插入的node的后一個(gè)是current。 } this.length++; console.log("Insert successfully!"); } else { throw new Error("插入的位置有誤啊!"); } }
RemoveNode
/** * @param {Number} position */ removeNode (position) { if (position > -1 && position < this.length) { let current = this.head, front, index = 0; if (!position) { // 位置為 0 。 this.head = current.next; current.next.previous = this.previous; } else { while (index++ < position) { front = current; current = current.next; } if (current.next) { // 這里判斷當(dāng)前node的下一個(gè)是否為 null。(例如要?jiǎng)h除最后一個(gè)是node.next是null的) current.next.previous = front; // 當(dāng)前node的下一個(gè)的previous為front。(有點(diǎn)繞口) } front.next = current.next; // 前一個(gè)的下一個(gè)為current的下一個(gè)。 (...繞口) } this.length--; console.log("Remove successfully!"); } else { throw new Error("移除的位置有誤??!"); } }
print () { let arr = [this.head]; let node = this.head; while (node.next) { node = node.next; arr.push(node); } arr.map( (x, index) => console.log(`第${index + 1}個(gè)節(jié)點(diǎn)是`, x)); }附上個(gè)人源碼
循環(huán)鏈表 ----(維基百科)
循環(huán)鏈表是一種鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu),它的最后一個(gè)結(jié)點(diǎn)指向頭結(jié)點(diǎn),形成一個(gè)環(huán)。因此,從循環(huán)鏈表中的任何一個(gè)結(jié)點(diǎn)出發(fā)都能找到任何其他結(jié)點(diǎn)。循環(huán)鏈表的操作和單鏈表的操作基本一致,差別僅僅在于算法中的循環(huán)條件有所不同。
1.單向循環(huán)鏈表
2.雙向循環(huán)鏈表
單向循環(huán)鏈表是在單鏈表基礎(chǔ)上,將最后一個(gè)節(jié)點(diǎn)的next指針指向鏈表頭(head)。
雙向循環(huán)鏈表是在雙向鏈表基礎(chǔ)上,將最后一個(gè)節(jié)點(diǎn)的next指針指向鏈表頭(head)。
就寫到這里咯。你問我怎么不實(shí)現(xiàn)循環(huán)鏈表?這個(gè)就...你們實(shí)現(xiàn)吧,程序猿嘛,要多動(dòng)手,多動(dòng)手。
謝謝大家。
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/90191.html
摘要:什么是雙鏈表上一篇實(shí)戰(zhàn)數(shù)據(jù)結(jié)構(gòu)基礎(chǔ)之單鏈表說到單鏈表由一個(gè)一個(gè)的作為節(jié)點(diǎn)的對(duì)象構(gòu)成的,每一個(gè)節(jié)點(diǎn)都有指向下一個(gè)節(jié)點(diǎn)的指針,最后一個(gè)節(jié)點(diǎn)的指針域指向空。 什么是雙鏈表? 上一篇實(shí)戰(zhàn)PHP數(shù)據(jù)結(jié)構(gòu)基礎(chǔ)之單鏈表說到 單鏈表由一個(gè)一個(gè)的作為節(jié)點(diǎn)的對(duì)象構(gòu)成的,每一個(gè)節(jié)點(diǎn)都有指向下一個(gè)節(jié)點(diǎn)的指針,最后一個(gè)節(jié)點(diǎn)的指針域指向空。每個(gè)節(jié)點(diǎn)可以存儲(chǔ)任何數(shù)據(jù)類型。 而雙鏈表每個(gè)節(jié)點(diǎn)有兩個(gè)指針域,分別指向前驅(qū)...
面試舊敵之紅黑樹(直白介紹深入理解) - Android - 掘金 讀完本文你將了解到: 什么是紅黑樹 黑色高度 紅黑樹的 5 個(gè)特性 紅黑樹的左旋右旋 指定節(jié)點(diǎn) x 的左旋 右圖轉(zhuǎn)成左圖 指定節(jié)點(diǎn) y 的右旋左圖轉(zhuǎn)成右圖 紅黑樹的平衡插入 二叉查找樹的插入 插入后調(diào)整紅黑樹結(jié)構(gòu) 調(diào)整思想 插入染紅后... java 多線程同步以及線程間通信詳解 & 消費(fèi)者生產(chǎn)者模式 & 死鎖 & Thread...
摘要:一個(gè)單向鏈表包含兩個(gè)值當(dāng)前節(jié)點(diǎn)的值和一個(gè)指向下一個(gè)節(jié)點(diǎn)的鏈接。為退出循環(huán)即已到了最后一個(gè)節(jié)點(diǎn)那么它的為加入節(jié)點(diǎn)后,要把單鏈表長度加一。第個(gè)節(jié)點(diǎn)是運(yùn)行結(jié)果個(gè)人源碼地址單鏈表就寫到這里咯,接下來還會(huì)寫其他的數(shù)據(jù)結(jié)構(gòu)本人也在學(xué)習(xí)當(dāng)中。 維基百科 鏈表是一種常見的基礎(chǔ)數(shù)據(jù)結(jié)構(gòu),是一種線性表,但是并不會(huì)按線性的順序存儲(chǔ)數(shù)據(jù),而是在每一個(gè)節(jié)點(diǎn)里存到下一個(gè)節(jié)點(diǎn)的指針。 鏈表中最簡(jiǎn)單的一種是單向鏈...
摘要:關(guān)于遞歸這里提一兩點(diǎn)遞歸基本有這幾步遞歸的模板,終止條件,遞歸調(diào)用,邏輯處理。 ?作者簡(jiǎn)介:大家好,我是車神哥,府學(xué)路18號(hào)的車神? ?個(gè)人主頁:應(yīng)無所住而生...
閱讀 3068·2023-04-25 17:22
閱讀 1624·2019-08-30 15:54
閱讀 1340·2019-08-30 15:53
閱讀 1895·2019-08-30 15:43
閱讀 3192·2019-08-29 12:29
閱讀 1299·2019-08-26 11:37
閱讀 3365·2019-08-23 18:02
閱讀 1693·2019-08-23 14:15