JavaScript資料結構之雙向連結串列
阿新 • • 發佈:2021-03-10
單向連結串列在遍歷時只能從頭到尾或者從尾遍歷到頭;所以單向連結串列可以輕鬆到達下一節點,但是回到上一個節點是很困難的
而雙向連結串列既可以從頭遍歷到尾,又可以從尾遍歷到頭,連結串列的相聯是雙向的,一個節點既有向前連線的引用,也有向後連線的引用
但是正因如此,雙向連結串列在插入或者刪除某個節點時,需要處理四個節點的引用,並且所佔hrxcHAQS用記憶體空間也更大一些
雙向連結串列實現
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 = newNo程式設計客棧de 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() } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。