国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

JavaScript數據結構之雙向鏈表

瀏覽:88日期:2023-06-07 13:23:49

單向鏈表在遍歷時只能從頭到尾或者從尾遍歷到頭;所以單向鏈表可以輕松到達下一節點,但是回到上一個節點是很困難的

而雙向鏈表既可以從頭遍歷到尾, 又可以從尾遍歷到頭,鏈表的相聯是雙向的,一個節點既有向前連接的引用,也有向后連接的引用

但是正因如此,雙向鏈表在插入或者刪除某個節點時,需要處理四個節點的引用,并且所占用內存空間也更大一些

JavaScript數據結構之雙向鏈表

雙向鏈表實現

JavaScript 代碼實現雙向鏈表

// 創建雙向鏈表的構造函數function DoublyLinkedList() { // 創建節點構造函數 function Node(element) { this.element = element this.next = null this.prev = null // 新添加的 } // 定義屬性 this.length = 0 this.head = null this.tail = null // 新添加的 // 定義相關操作方法 // 在尾部追加數據 DoublyLinkedList.prototype.append = function (element) { // 1.根據元素創建節點 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++ } // 在任意位置插入數據 DoublyLinkedList.prototype.insert = function (position, element) { // 1.判斷越界的問題 if (position < 0 || position > this.length) return false // 2.創建新的節點 var newNode = new Node(element) // 3.判斷插入的位置 if (position === 0) { // 在第一個位置插入數據 // 判斷鏈表是否為空 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 { // 在中間位置插入數據 // 定義屬性 var index = 0 var current = this.head var previous = null // 查找正確的位置 while (index++ < position) { previous = current current = current.next } // 交換節點的指向順序 newNode.next = current newNode.prev = previous current.prev = newNode previous.next = newNode } // 4.length+1 this.length++ return true } // 根據位置刪除對應的元素 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 } // 根據元素獲取在鏈表中的位置 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.來到這個位置, 說明沒有找到, 則返回-1 return -1 } // 根據元素刪除 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 } // 獲取第一個元素 DoublyLinkedList.prototype.getHead = function () { return this.head.element } // 獲取最后一個元素 DoublyLinkedList.prototype.getTail = function () { return this.tail.element } // 遍歷方法的實現 // 正向遍歷的方法 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) } // 實現toString方法 DoublyLinkedList.prototype.toString = function () { return this.forwardString() }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 99在线精品视频免费观里 | 国产欧美一区二区三区在线看 | 草草视频手机在线观看视频 | 亚洲国产成人精品久久 | 国产精品专区第二 | 久草在线视频资源 | 国产成人一区二区三区在线视频 | 99精彩视频 | 女人叉开腿让男人捅 | 亚洲三级在线看 | 全免费a级毛片免费看 | 97在线视频免费播放 | 亚洲国产精品综合久久一线 | 国产一级毛片夜一级毛片 | 毛片在线视频 | 91精品手机国产露脸 | 香蕉香蕉国产片一级一级毛片 | 一级片免| 欧美日韩视频一区三区二区 | 亚洲日本高清影院毛片 | 久久久久国产精品免费 | 欧美日韩一区二区三区视频在线观看 | 日韩精品欧美激情国产一区 | 国产午夜精品一区二区三区不卡 | 三级视频网站在线观看 | 国产黄页 | 91久久亚洲精品国产一区二区 | 国产97在线视频 | 国产成人亚洲精品 | 在线观看精品自拍视频 | 国产成人av性色在线影院 | 9久久99久久久精品齐齐综合色圆 | 一级一级一片在线观看 | 香蕉久久一区二区不卡无毒影院 | 不卡一区二区在线观看 | 福利片免费一区二区三区 | 欧美人成在线观看 | 久久亚洲精品国产精品777777 | 天天爱天天做天天爽天天躁 | 久久这里有精品视频 | 成人一级片在线观看 |