亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

前言

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

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

GitHub源碼地址,下載可運行

實現(xiàn)

  • 通過LinkedList的類創(chuàng)建鏈表實例,鏈表下有添加,查找,刪除,顯示節(jié)點等方法
  • 鏈表初始默認(rèn)有一個"_head"頭部節(jié)點,使用時隱藏
  • 按元素/索引 添加、刪除,未找到時返回錯誤,查找未找到時返回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元素查找上一個節(jié)點元素

添加

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

刪除

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

其他

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

方法代碼

鏈表類LinkedList

 function LinkedList (...rest) {
 this._head = new Node('_head') // 鏈表頭節(jié)點
 // 如果new時有傳進值,則添加到實例中
 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é)點類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é)點,出去頭結(jié)點
 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é)點item的上一個節(jié)點,未找到返回-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é)點,找到要插入到的item的節(jié)點位置,把新節(jié)點插到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é)點`)
 }
 }

obj.insertIndex(index,newElement)

// 插入節(jié)點,新節(jié)點插到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é)點`)
 }
 }

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

obj.removeIndex(index)

// 刪除某個索引下的節(jié)點
 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é)點`)
 }
 }

obj.size()

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

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
 }

實例測試

 // 運行測試
 let obj = new LinkedList('節(jié)點0', '節(jié)點1', '節(jié)點2', '節(jié)點3', '節(jié)點4', '節(jié)點5')
 console.log('---實例對象')
 console.log(obj)
 console.log('---末尾插入元素')
 obj.push('push插入')
 console.log(obj.display())
 console.log('---元素后插入元素')
 obj.insert('元素插入', '節(jié)點2')
 console.log(obj.display())
 console.log('---索引處插入元素')
 obj.insertIndex('索引插入', 5)
 console.log(obj.display())
 console.log('---查找元素位置')
 console.log(obj.find('節(jié)點4'))
 console.log('---移除元素')
 obj.remove('節(jié)點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é)點3'))
 console.log('---反轉(zhuǎn)鏈表')
 obj.reversal()
 console.log(obj.display())

測試結(jié)果

結(jié)尾

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

相關(guān)鏈接

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

總結(jié)

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

相關(guān)文章

  • js實現(xiàn)鼠標(biāo)劃過給div加透明度的方法

    js實現(xiàn)鼠標(biāo)劃過給div加透明度的方法

    這篇文章主要介紹了js實現(xiàn)鼠標(biāo)劃過給div加透明度的方法,涉及javascript動態(tài)操作頁面元素屬性的相關(guān)技巧,該方法可兼容火狐與IE瀏覽器,需要的朋友可以參考下
    2015-05-05
  • JS操作數(shù)據(jù)庫的實例代碼

    JS操作數(shù)據(jù)庫的實例代碼

    這篇文章介紹了JS操作數(shù)據(jù)庫的實例代碼,有需要的朋友可以參考一下
    2013-10-10
  • 精通JavaScript的this關(guān)鍵字

    精通JavaScript的this關(guān)鍵字

    這篇文章主要介紹了JavaScript的this關(guān)鍵字,真正幫助大家做到精通this關(guān)鍵字,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-02-02
  • d3.js中冷門卻實用的內(nèi)置函數(shù)總結(jié)

    d3.js中冷門卻實用的內(nèi)置函數(shù)總結(jié)

    D3.js是一個JavaScript庫,它可以通過數(shù)據(jù)來操作文檔。D3可以通過使用HTML、SVG和CSS把數(shù)據(jù)鮮活形象地展現(xiàn)出來。d3.js其實提供了很多內(nèi)置的函數(shù),可以卻被大家忽略了,下面這篇文章就來給大家詳細(xì)介紹了d3.js中冷門卻實用的一些內(nèi)置函數(shù),需要的朋友可以參考借鑒。
    2017-02-02
  • JavaScript實現(xiàn)簡易計算器小功能

    JavaScript實現(xiàn)簡易計算器小功能

    這篇文章主要為大家詳細(xì)介紹了JavaScript實現(xiàn)簡易計算器小功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • 可視化埋點平臺元素曝光采集intersectionObserver思路實踐

    可視化埋點平臺元素曝光采集intersectionObserver思路實踐

    這篇文章主要為大家介紹了可視化埋點平臺元素曝光采集的思路—intersectionObserver的實戰(zhàn)經(jīng)驗詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • JavaScript設(shè)計模式中的觀察者模式

    JavaScript設(shè)計模式中的觀察者模式

    這篇文章主要介紹了JavaScript設(shè)計模式中的觀察者模式,觀察者設(shè)計模式適用于監(jiān)聽一對多的操作,例如監(jiān)聽對象屬性的修改等等,觀察者模式能夠降低代碼耦合度,提升可擴展性
    2022-06-06
  • 微信小程序?qū)崿F(xiàn)搜索框功能

    微信小程序?qū)崿F(xiàn)搜索框功能

    這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)搜索框功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • JS數(shù)據(jù)雙向綁定原理與用法實例分析

    JS數(shù)據(jù)雙向綁定原理與用法實例分析

    這篇文章主要介紹了JS數(shù)據(jù)雙向綁定原理與用法,結(jié)合實例形式分析了JavaScript數(shù)據(jù)雙向綁定相關(guān)原理、實現(xiàn)技巧與操作注意事項,需要的朋友可以參考下
    2019-11-11
  • JS實現(xiàn)非首屏圖片延遲加載的示例

    JS實現(xiàn)非首屏圖片延遲加載的示例

    下面小編就為大家分享一篇用JS實現(xiàn)非首屏圖片延遲加載的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01

最新評論