最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

如何用JavaScript實(shí)現(xiàn)功能齊全的單鏈表詳解

 更新時(shí)間:2019年02月11日 10:47:35   作者:王文健  
這篇文章主要給大家介紹了關(guān)于如何用JavaScript實(shí)現(xiàn)功能齊全的單鏈表的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

前言

前端也要搞好數(shù)據(jù)結(jié)構(gòu)哦!

用JavaScript實(shí)現(xiàn)了個(gè)單鏈表,通過LinkedList構(gòu)造函數(shù)可實(shí)例化一個(gè)單鏈表數(shù)據(jù)結(jié)構(gòu)的對象,所有的方法放到LinkedList構(gòu)造函數(shù)的原型對象上,寫了暫時(shí)能想到的所有方法

GitHub源碼地址,下載可運(yùn)行

實(shí)現(xiàn)

  • 通過LinkedList的類創(chuàng)建鏈表實(shí)例,鏈表下有添加,查找,刪除,顯示節(jié)點(diǎn)等方法
  • 鏈表初始默認(rèn)有一個(gè)"_head"頭部節(jié)點(diǎn),使用時(shí)隱藏
  • 按元素/索引 添加、刪除,未找到時(shí)返回錯(cuò)誤,查找未找到時(shí)返回null或-1
  • let obj = new LinkedList()

方法介紹

查找

  • obj.find(item)通過item元素內(nèi)容查找到該元素
  • obj.findIndex(index)通過index索引查找到該元素
  • obj.findIndexOf(item)通過item元素內(nèi)容查找到該元素索引
  • obj.findPrev(item)通過item元素查找上一個(gè)節(jié)點(diǎn)元素

添加

  • obj.insert(item,newElement)在item元素后插入新元素
  • obj.push(item)在鏈表末尾插入item元素
  • obj.insertIndex(index,newElement)在index索引處插入新元素

刪除

  • obj.remove(item)刪除item元素
  • obj.removeIndex(index)刪除index索引處節(jié)點(diǎn)

其他

  • obj.size()返回該鏈表的長度
  • obj.display()數(shù)組形式返回該鏈表,便于觀察,測試
  • obj.reversal()鏈表順序反轉(zhuǎn)(遞歸)

方法代碼

鏈表類LinkedList

 function LinkedList (...rest) {
 this._head = new Node('_head') // 鏈表頭節(jié)點(diǎn)
 // 如果new時(shí)有傳進(jìn)值,則添加到實(shí)例中
 if (rest.length) {
 this.insert(rest[0], '_head')
 for (let i = 1; i < rest.length; i++) {
 this.insert(rest[i], rest[i - 1])
 }
 }
 }
 LinkedList.prototype.find = find
 LinkedList.prototype.findPrev = findPrev
 LinkedList.prototype.findIndex = findIndex
 LinkedList.prototype.findIndexOf = findIndexOf
 LinkedList.prototype.push = push
 LinkedList.prototype.insert = insert
 LinkedList.prototype.insertIndex = insertIndex
 LinkedList.prototype.remove = remove
 LinkedList.prototype.removeIndex = removeIndex
 LinkedList.prototype.size = size
 LinkedList.prototype.display = display
 LinkedList.prototype.reversal = reversal

創(chuàng)建新節(jié)點(diǎn)類Node

 function Node (element) {
 this.element = element
 this.next = null
 }

obj.find(item)

// 查找函數(shù),在鏈表中查找item的位置,并把它返回,未找到返回-1
 function find (item) {
 let currNode = this._head
 while (currNode !== null && currNode.element !== item) {
 currNode = currNode.next
 }
 if (currNode !== null) {
 return currNode
 } else {
 return null
 }
 }

obj.findIndex(index)

// 通過元素的索引返回該元素
 function findIndex (index) {
 let currNode = this._head
 let tmpIndex = 0
 while (currNode !== null) {
 // 找到該index位置,返回當(dāng)前節(jié)點(diǎn),出去頭結(jié)點(diǎn)
 if (tmpIndex === index + 1) {
 return currNode
 }
 tmpIndex += 1
 currNode = currNode.next
 }
 return null
 }

obj.findIndexOf(item)

 function findIndexOf (item) {
 let currNode = this._head
 let tmpIndex = 0
 while (currNode.next !== null && currNode.next.element !== item) {
 tmpIndex += 1
 currNode = currNode.next
 }
 if (currNode !== null) {
 return tmpIndex
 } else {
 return -1
 }
 }

obj.findPrev(item)

// 尋找目標(biāo)節(jié)點(diǎn)item的上一個(gè)節(jié)點(diǎn),未找到返回-1
 function findPrev (item) {
 let currNode = this._head
 while (currNode.next !== null && currNode.next.element !== item) {
 currNode = currNode.next
 }
 if (currNode.next !== item) {
 return currNode
 } else {
 return null
 }
 }

obj.insert(item,newElement)

// 插入節(jié)點(diǎn),找到要插入到的item的節(jié)點(diǎn)位置,把新節(jié)點(diǎn)插到item后面
 function insert (newElement, item) {
 let newNode = new Node(newElement)
 let currNode = this.find(item)
 if (currNode) {
 newNode.next = currNode.next
 currNode.next = newNode
 } else {
 console.error(`insert error:鏈表中不存在「${item}」節(jié)點(diǎn)`)
 }
 }

obj.insertIndex(index,newElement)

// 插入節(jié)點(diǎn),新節(jié)點(diǎn)插到index索引下
 function insertIndex (newElement, index) {
 let newNode = new Node(newElement)
 let currNode = this.findIndex(index)
 if (currNode) {
 newNode.next = currNode.next
 currNode.next = newNode
 } else {
 console.error(`insertIndex error:鏈表中不存在「${index}」索引節(jié)點(diǎn)`)
 }
 }

obj.push(item)

// 在鏈表最后一位添加元素
 function push (element) {
 let newNode = new Node(element)
 let currNode = this._head
 while (currNode.next !== null) {
 currNode = currNode.next
 }
 currNode.next = newNode
 }

obj.remove(item)

// 刪除節(jié)點(diǎn),找到刪除的位置,刪除,未找到提示錯(cuò)誤
 function remove (item) {
 // 找到當(dāng)前和上一個(gè)節(jié)點(diǎn),讓上一個(gè)節(jié)點(diǎn)的next指向item下一個(gè)節(jié)點(diǎn)
 let tmpPrev = this.findPrev(item)
 let tmpNext = this.find(item)
 if (tmpPrev && tmpNext) {
 tmpPrev.next = tmpNext.next
 } else {
 console.error(`remove error:鏈表中不存在「${item}」節(jié)點(diǎn)`)
 }
 }

obj.removeIndex(index)

// 刪除某個(gè)索引下的節(jié)點(diǎn)
 function removeIndex (index) {
 let tmpPrev = this.findIndex(index - 1)
 let currNode = this.findIndex(index)
 if (tmpPrev && currNode) {
 tmpPrev.next = currNode.next
 } else {
 console.error(`removeIndex error:鏈表中不存在「${index}」索引節(jié)點(diǎn)`)
 }
 }

obj.size()

 function size () {
 let currNode = this._head
 let tmpSize = 0
 while (currNode.next !== null) {
 tmpSize += 1
 currNode = currNode.next
 }
 return tmpSize // 不計(jì)算頭部節(jié)點(diǎn)
 }

obj.reversal()

 // 鏈表反轉(zhuǎn)=>遞歸
 function reversal () {
 function reversalList (item) {
 if (item.next) {
 let tmpItem = reversalList(item.next)
 item.next = null
 tmpItem.next = item
 return item
 } else {
 obj._head.next = item
 return item
 }
 }
 reversalList(obj._head.next)
 }

obj.display()

 function display () {
 // 鏈表展示和使用,默認(rèn)頭部不存在
 let currNode = this._head.next
 let tmpArr = []
 while (currNode !== null) {
 tmpArr.push(currNode)
 currNode = currNode.next
 }
 return tmpArr
 }

實(shí)例測試

 // 運(yùn)行測試
 let obj = new LinkedList('節(jié)點(diǎn)0', '節(jié)點(diǎn)1', '節(jié)點(diǎn)2', '節(jié)點(diǎn)3', '節(jié)點(diǎn)4', '節(jié)點(diǎn)5')
 console.log('---實(shí)例對象')
 console.log(obj)
 console.log('---末尾插入元素')
 obj.push('push插入')
 console.log(obj.display())
 console.log('---元素后插入元素')
 obj.insert('元素插入', '節(jié)點(diǎn)2')
 console.log(obj.display())
 console.log('---索引處插入元素')
 obj.insertIndex('索引插入', 5)
 console.log(obj.display())
 console.log('---查找元素位置')
 console.log(obj.find('節(jié)點(diǎn)4'))
 console.log('---移除元素')
 obj.remove('節(jié)點(diǎn)5')
 console.log(obj.display())
 console.log('---移除索引元素')
 obj.removeIndex(5)
 console.log(obj.display())
 console.log('---元素長度')
 console.log(obj.size())
 console.log('---索引查找')
 console.log(obj.findIndex(2))
 console.log('---元素查找索引')
 console.log(obj.findIndexOf('節(jié)點(diǎn)3'))
 console.log('---反轉(zhuǎn)鏈表')
 obj.reversal()
 console.log(obj.display())

測試結(jié)果

結(jié)尾

最近遇到單鏈表反轉(zhuǎn)的問題,所有加了一個(gè)單鏈表反轉(zhuǎn)的方法,用遞歸實(shí)現(xiàn)

相關(guān)鏈接

實(shí)現(xiàn)單鏈表反轉(zhuǎn)的幾種方法

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • js實(shí)現(xiàn)放大鏡特效

    js實(shí)現(xiàn)放大鏡特效

    這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)放大鏡特效,簡單實(shí)用的代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • 微信小程序?qū)崿F(xiàn)自定義拍攝組件

    微信小程序?qū)崿F(xiàn)自定義拍攝組件

    這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)自定義拍攝組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • 關(guān)于js內(nèi)存泄露的一個(gè)好例子

    關(guān)于js內(nèi)存泄露的一個(gè)好例子

    這篇文章主要介紹了關(guān)于js內(nèi)存泄露的一個(gè)好例子。需要的朋友可以過來參考下,希望對大家有所幫助
    2013-12-12
  • JS前端開發(fā)之exec()和match()的對比使用

    JS前端開發(fā)之exec()和match()的對比使用

    match()方法可在字符串內(nèi)檢索指定的值,或找到一個(gè)或多個(gè)正則表達(dá)式的匹配,下面這篇文章主要給大家介紹了關(guān)于JS前端開發(fā)之exec()和match()的對比使用的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • JavaScript ES6 Class類實(shí)現(xiàn)原理詳解

    JavaScript ES6 Class類實(shí)現(xiàn)原理詳解

    這篇文章主要介紹了JavaScript ES6 Class類實(shí)現(xiàn)原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • checkbox設(shè)置復(fù)選框的只讀效果不讓用戶勾選

    checkbox設(shè)置復(fù)選框的只讀效果不讓用戶勾選

    有時(shí)候是只想告知用戶這個(gè)地方是可以進(jìn)行勾選操作的而不想讓用戶在此處勾選(比如在信息展示頁面),這時(shí)候就需要將復(fù)選框設(shè)置成只讀的效果,具體實(shí)現(xiàn)方法如下
    2013-08-08
  • 詳解JavaScript的懶加載是如何實(shí)現(xiàn)的

    詳解JavaScript的懶加載是如何實(shí)現(xiàn)的

    懶加載(Lazy Loading)是一種在軟件開發(fā)中常用的優(yōu)化技術(shù),它主要用于延遲加載資源,直到真正需要使用的時(shí)候才進(jìn)行加載,這樣可以減少初始加載的時(shí)間和資源消耗,并提升用戶體驗(yàn),本文給大家詳細(xì)介紹了JavaScript的懶加載是如何實(shí)現(xiàn)的,需要的朋友可以參考下
    2024-01-01
  • js實(shí)現(xiàn)貪吃蛇游戲(簡易版)

    js實(shí)現(xiàn)貪吃蛇游戲(簡易版)

    這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)貪吃蛇游戲簡易版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • 在線所見即所得HTML編輯器的實(shí)現(xiàn)原理淺析

    在線所見即所得HTML編輯器的實(shí)現(xiàn)原理淺析

    這篇文章主要介紹了在線所見即所得HTML編輯器的實(shí)現(xiàn)原理淺析,本文用初始化、打開編輯功能、獲取編輯器的內(nèi)容、增加樣式設(shè)置、再進(jìn)一步等步驟闡述在線編輯器的基本實(shí)現(xiàn)原理,需要的朋友可以參考下
    2015-04-04
  • IE瀏覽器IFrame對象內(nèi)存不釋放問題解決方法

    IE瀏覽器IFrame對象內(nèi)存不釋放問題解決方法

    IFrame對象占用的內(nèi)存資源在窗體關(guān)閉后不會釋放。彈出關(guān)閉反復(fù)多次后,IE瀏覽器內(nèi)存占用可超過數(shù)百M(fèi),嚴(yán)重時(shí)IE瀏覽器報(bào)錯(cuò)
    2014-08-08

最新評論

通化县| 广元市| 灌南县| 河东区| 崇阳县| 苏州市| 宝应县| 彰化市| 龙里县| 鄂伦春自治旗| 郯城县| 牟定县| 商水县| 斗六市| 庄河市| 定边县| 城固县| 通海县| 同江市| 青岛市| 浪卡子县| 犍为县| 长春市| 商河县| 砀山县| 嘉禾县| 疏附县| 洛南县| 巨野县| 凤阳县| 瑞金市| 涡阳县| 陵水| 鞍山市| 进贤县| 镇安县| 徐闻县| 栖霞市| 遵义市| 宁化县| 华池县|