javascript
JavaScript数据结构与算法——链表详解(下)
在JavaScript數(shù)據(jù)結(jié)構(gòu)與算法——鏈表詳解(上)中,我們探討了一下鏈表的定義、實(shí)現(xiàn)原理以及單鏈表的實(shí)現(xiàn)。接下來(lái)我們進(jìn)一步了解一下鏈表的其他內(nèi)容。
1、雙向鏈表
雙向鏈表實(shí)現(xiàn)原理圖:
與單向鏈表不同的是,圖中增加了小線(xiàn)部分,每一個(gè)節(jié)點(diǎn)增加了一個(gè)指向前驅(qū)節(jié)點(diǎn)的屬性,這樣就實(shí)現(xiàn)了雙向鏈表。
雙向鏈表中,刪除節(jié)點(diǎn)不需要知道待刪除節(jié)點(diǎn)的前驅(qū)節(jié)點(diǎn),只要把待刪除節(jié)點(diǎn)指向前驅(qū)節(jié)點(diǎn)的屬性指向NULL,同時(shí)把前驅(qū)節(jié)點(diǎn)指向待刪除節(jié)點(diǎn)的后繼節(jié)點(diǎn)即可,這樣在刪除節(jié)點(diǎn)時(shí)更高效。結(jié)合原理圖比較好理解一點(diǎn),雙向鏈表中,刪除節(jié)點(diǎn)原理如下圖:
增加節(jié)點(diǎn)原理如下圖:
2、JS實(shí)現(xiàn)雙向鏈表
以下代碼基于JavaScript數(shù)據(jù)結(jié)構(gòu)與算法——鏈表詳解(上)中的代碼修改,文章末尾會(huì)附上完整代碼。
從原理圖可知,我們應(yīng)該先給節(jié)點(diǎn)添加previous屬性,可通過(guò)修改Node類(lèi)實(shí)現(xiàn):
2.1、Node類(lèi)修改
// Created by xiaoqiang on 24/04/2018. class Node {constructor (element) {this.element = elementthis.next = nullthis.previous = null} }2.2、LinkedList類(lèi)修改
class LinkedList {constructor () {this.header = new Node('header') // 頭節(jié)點(diǎn)}// 查找元素find (item) {let currNode = this.headerwhile (currNode.element !== item) {currNode = currNode.next}return currNode}// 用于返回最后一個(gè)節(jié)點(diǎn)findLast () {let currNode = this.headerwhile (currNode.next !== null) {currNode = currNode.next}return currNode}// 插入節(jié)點(diǎn)insert (item, newElement) {let newNode = new Node(newElement)let current = this.find(item)newNode.next = current.nextnewNode.previous = currentcurrent.next = newNode}// 刪除節(jié)點(diǎn)remove (item) {let currNode = this.find(item)if (currNode.next !== null) {currNode.previous.next = currNode.nextcurrNode.next.previous = currNode.previouscurrNode.next = nullcurrNode.previous = null}}// 顯示鏈表display () {let currNode = this.headerwhile(currNode.next !== null) {console.log(currNode.next.element)currNode = currNode.next}}// 反向顯示鏈表displayReverse () {let currNode = this.headercurrNode = this.findLast()while (currNode.previous !== null) {console.log(currNode.element)currNode = currNode.previous}} }鏈表中的方法可根據(jù)需要添加。
2.3、測(cè)試實(shí)例修改:
let books = new LinkedList() console.log('開(kāi)始插入操作') books.insert('header', 'JavaScript') books.insert('JavaScript', 'PHP') books.insert('PHP', 'Nodejs') console.log('插入后的鏈表數(shù)據(jù)為:') books.display() console.log('開(kāi)始進(jìn)行移除操作') books.remove('PHP') console.log('移除后PHP后的鏈表的數(shù)據(jù)為:') books.display() console.log('反向顯示鏈表:') books.displayReverse()2.3、雙向鏈表完整代碼
// Created by xiaoqiang on 24/04/2018. class Node {constructor (element) {this.element = elementthis.next = nullthis.previous = null} }class LinkedList {constructor () {this.header = new Node('header') // 頭節(jié)點(diǎn)}// 查找元素find (item) {let currNode = this.headerwhile (currNode.element !== item) {currNode = currNode.next}return currNode}// 用于返回最后一個(gè)節(jié)點(diǎn)findLast () {let currNode = this.headerwhile (currNode.next !== null) {currNode = currNode.next}return currNode}// 插入節(jié)點(diǎn)insert (item, newElement) {let newNode = new Node(newElement)let current = this.find(item)newNode.next = current.nextnewNode.previous = currentcurrent.next = newNode}// 刪除節(jié)點(diǎn)remove (item) {let currNode = this.find(item)if (currNode.next !== null) {currNode.previous.next = currNode.nextcurrNode.next.previous = currNode.previouscurrNode.next = nullcurrNode.previous = null}}// 顯示鏈表display () {let currNode = this.headerwhile(currNode.next !== null) {console.log(currNode.next.element)currNode = currNode.next}}// 反向顯示鏈表displayReverse () {let currNode = this.headercurrNode = this.findLast()while (currNode.previous !== null) {console.log(currNode.element)currNode = currNode.previous}} }let books = new LinkedList() console.log('開(kāi)始插入操作') books.insert('header', 'JavaScript') books.insert('JavaScript', 'PHP') books.insert('PHP', 'Nodejs') console.log('插入后的鏈表數(shù)據(jù)為:') books.display() console.log('開(kāi)始進(jìn)行移除操作') books.remove('PHP') console.log('移除后PHP后的鏈表的數(shù)據(jù)為:') books.display() console.log('反向顯示鏈表:') books.displayReverse()2.4、運(yùn)行及結(jié)果
使用node運(yùn)行此js文件,結(jié)果如下:
3、循環(huán)鏈表
循環(huán)鏈表實(shí)現(xiàn)原理如下圖:
原理比雙向鏈表更簡(jiǎn)單一些,有興趣可以嘗試實(shí)現(xiàn)一下,也可以給以上實(shí)現(xiàn)的鏈表類(lèi)多添加一些方法,加深理解。
至此,JavaScript數(shù)據(jù)結(jié)構(gòu)與算法——鏈表詳解(下)完結(jié),有錯(cuò)誤歡迎指出。
總結(jié)
以上是生活随笔為你收集整理的JavaScript数据结构与算法——链表详解(下)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: JavaScript数据结构与算法——链
- 下一篇: 人为什么打哈欠(为什么看到别人打哈欠就打