欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript數(shù)據(jù)結(jié)構(gòu)之雙向鏈表

 更新時(shí)間:2021年03月07日 15:28:50   作者:希魔王的塔羅牌  
這篇文章主要為大家詳細(xì)介紹了JavaScript數(shù)據(jù)結(jié)構(gòu)之雙向鏈表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

單向鏈表在遍歷時(shí)只能從頭到尾或者從尾遍歷到頭;所以單向鏈表可以輕松到達(dá)下一節(jié)點(diǎn),但是回到上一個(gè)節(jié)點(diǎn)是很困難的

而雙向鏈表既可以從頭遍歷到尾, 又可以從尾遍歷到頭,鏈表的相聯(lián)是雙向的,一個(gè)節(jié)點(diǎn)既有向前連接的引用,也有向后連接的引用

但是正因如此,雙向鏈表在插入或者刪除某個(gè)節(jié)點(diǎn)時(shí),需要處理四個(gè)節(jié)點(diǎn)的引用,并且所占用內(nèi)存空間也更大一些

雙向鏈表實(shí)現(xiàn)

JavaScript 代碼實(shí)現(xiàn)雙向鏈表

// 創(chuàng)建雙向鏈表的構(gòu)造函數(shù)
function DoublyLinkedList() {
 // 創(chuàng)建節(jié)點(diǎn)構(gòu)造函數(shù)
 function Node(element) {
  this.element = element
  this.next = null
  this.prev = null // 新添加的
 }

 // 定義屬性
 this.length = 0
 this.head = null
 this.tail = null // 新添加的

 // 定義相關(guān)操作方法
 // 在尾部追加數(shù)據(jù)
 DoublyLinkedList.prototype.append = function (element) {
  // 1.根據(jù)元素創(chuàng)建節(jié)點(diǎn)
  var newNode = new Node(element)

  // 2.判斷列表是否為空列表
  if (this.head == null) {
   this.head = newNode
   this.tail = newNode
  } else {
   this.tail.next = newNode
   newNode.prev = this.tail
   this.tail = newNode
  }

  // 3.length+1
  this.length++
 }

 // 在任意位置插入數(shù)據(jù)
 DoublyLinkedList.prototype.insert = function (position, element) {
  // 1.判斷越界的問題
  if (position < 0 || position > this.length) return false

  // 2.創(chuàng)建新的節(jié)點(diǎn)
  var newNode = new Node(element)

  // 3.判斷插入的位置
  if (position === 0) { // 在第一個(gè)位置插入數(shù)據(jù)
   // 判斷鏈表是否為空
   if (this.head == null) {
    this.head = newNode
    this.tail = newNode
   } else {
    this.head.prev = newNode
    newNode.next = this.head
    this.head = newNode
   }
  } else if (position === this.length) { // 插入到最后的情況
   // 思考: 這種情況是否需要判斷鏈表為空的情況呢? 答案是不需要, 為什么?
   this.tail.next = newNode
   newNode.prev = this.tail
   this.tail = newNode
  } else { // 在中間位置插入數(shù)據(jù)
   // 定義屬性
   var index = 0
   var current = this.head
   var previous = null

   // 查找正確的位置
   while (index++ < position) {
    previous = current
    current = current.next
   }

   // 交換節(jié)點(diǎn)的指向順序
   newNode.next = current
   newNode.prev = previous
   current.prev = newNode
   previous.next = newNode
  }

  // 4.length+1
  this.length++

  return true
 }

 // 根據(jù)位置刪除對應(yīng)的元素
 DoublyLinkedList.prototype.removeAt = function (position) {
  // 1.判斷越界的問題
  if (position < 0 || position >= this.length) return null

  // 2.判斷移除的位置
  var current = this.head
  if (position === 0) {
   if (this.length == 1) {
    this.head = null
    this.tail = null
   } else {
    this.head = this.head.next
    this.head.prev = null
   }
  } else if (position === this.length -1) {
   current = this.tail
   this.tail = this.tail.prev
   this.tail.next = null
  } else {
   var index = 0
   var previous = null

   while (index++ < position) {
    previous = current
    current = current.next
   }

   previous.next = current.next
   current.next.prev = previous
  }

  // 3.length-1
  this.length--

  return current.element
 }

 // 根據(jù)元素獲取在鏈表中的位置
 DoublyLinkedList.prototype.indexOf = function (element) {
  // 1.定義變量保存信息
  var current = this.head
  var index = 0

  // 2.查找正確的信息
  while (current) {
   if (current.element === element) {
    return index
   }
   index++
   current = current.next
  }

  // 3.來到這個(gè)位置, 說明沒有找到, 則返回-1
  return -1
 }

 // 根據(jù)元素刪除
 DoublyLinkedList.prototype.remove = function (element) {
  var index = this.indexOf(element)
  return this.removeAt(index)
 }

 // 判斷是否為空
 DoublyLinkedList.prototype.isEmpty = function () {
  return this.length === 0
 }

 // 獲取鏈表長度
 DoublyLinkedList.prototype.size = function () {
  return this.length
 }

 // 獲取第一個(gè)元素
 DoublyLinkedList.prototype.getHead = function () {
  return this.head.element
 }

 // 獲取最后一個(gè)元素
 DoublyLinkedList.prototype.getTail = function () {
  return this.tail.element
 }

 // 遍歷方法的實(shí)現(xiàn)
 // 正向遍歷的方法
 DoublyLinkedList.prototype.forwardString = function () {
  var current = this.head
  var forwardStr = ""

  while (current) {
   forwardStr += "," + current.element
   current = current.next
  }

  return forwardStr.slice(1)
 }

 // 反向遍歷的方法
 DoublyLinkedList.prototype.reverseString = function () {
  var current = this.tail
  var reverseStr = ""

  while (current) {
   reverseStr += "," + current.element
   current = current.prev
  }

  return reverseStr.slice(1)
 }

 // 實(shí)現(xiàn)toString方法
 DoublyLinkedList.prototype.toString = function () {
  return this.forwardString()
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • js自定義trim函數(shù)實(shí)現(xiàn)刪除兩端空格功能

    js自定義trim函數(shù)實(shí)現(xiàn)刪除兩端空格功能

    這篇文章主要介紹了js自定義trim函數(shù)實(shí)現(xiàn)刪除兩端空格功能,結(jié)合實(shí)例形式分析了javascript基于正則替換實(shí)現(xiàn)類似trim函數(shù)刪除字符串兩端空格的相關(guān)操作技巧,并附帶jQuery類似功能函數(shù)使用方法,需要的朋友可以參考下
    2018-02-02
  • es6系列教程_ Map詳解以及常用api介紹

    es6系列教程_ Map詳解以及常用api介紹

    下面小編就為大家?guī)硪黄猠s6系列教程_ Map詳解以及常用api介紹。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09
  • uniapp跨頁面?zhèn)鲄⒌膸追N方式小結(jié)

    uniapp跨頁面?zhèn)鲄⒌膸追N方式小結(jié)

    當(dāng)我們在開發(fā)Uni-app應(yīng)用時(shí),經(jīng)常會(huì)遇到需要在不同頁面之間傳遞參數(shù)的情況,為了實(shí)現(xiàn)跨頁面?zhèn)鲄?Uni-app提供了幾種方式,所以本文小編將給大家介紹一下uniapp跨頁面?zhèn)鲄⒌膸追N方式,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下
    2024-01-01
  • Javascript調(diào)試之console對象——你不知道的一些小技巧

    Javascript調(diào)試之console對象——你不知道的一些小技巧

    這篇文章主要總結(jié)了console對象的一些有用的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2017-07-07
  • 詳解JavaScript正則表達(dá)式之分組匹配及反向引用

    詳解JavaScript正則表達(dá)式之分組匹配及反向引用

    這篇文章主要介紹了詳解JavaScript正則表達(dá)式之分組匹配及反向引用 的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • 解讀請求方式Method和請求類型Content-Type

    解讀請求方式Method和請求類型Content-Type

    HTTP請求中,Content-Type頭部用于指定請求體或響應(yīng)體的類型,常見的有application/x-www-form-urlencoded、multipart/form-data、application/json、text/plain、application/xml等,常用請求方式包括Get、Post、Put、Delete
    2024-09-09
  • DatePickerDialog 自定義樣式及使用全解

    DatePickerDialog 自定義樣式及使用全解

    這篇文章主要介紹了DatePickerDialog 自定義樣式及使用全解,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-07-07
  • JS實(shí)現(xiàn)隨機(jī)抽取三人

    JS實(shí)現(xiàn)隨機(jī)抽取三人

    這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)隨機(jī)抽取三人,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • 如何寫出優(yōu)雅的JS 代碼

    如何寫出優(yōu)雅的JS 代碼

    在開發(fā)中,變量名,函數(shù)名一般要做到清晰明了,盡量做到看名字就能讓人知道你的意圖,所以變量和函數(shù)命名是挺重要,今天來看看如果較優(yōu)雅的方式給變量和函數(shù)命名。
    2021-05-05
  • el-dialog對話框子組件的關(guān)閉方法

    el-dialog對話框子組件的關(guān)閉方法

    這篇文章主要介紹了el-dialog對話框子組件的關(guān)閉方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-12-12

最新評論