JavaSctipt實現雙向鏈表

一.什麼是雙向鏈表?

雙向鏈表,又稱爲雙鏈表,是鏈表的一種,它的每個數據結點中都有兩個指針,分別指向直接後繼和直接前驅。所以,從雙向鏈表中的任意一個結點開始,都可以很方便地訪問它的前驅結點和後繼結點。一般我們都構造雙向循環鏈表——維基百科

參考文章

二.代碼實現

<!--
 * @Description: 雙向鏈表的封裝
 * @Autor: wangxin
 * @Date: 2020-05-30 06:40:13
 * @LastEditors: Seven
 * @LastEditTime: 2020-05-30 14:00:33
-->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 封裝雙向鏈表
      function DoublyLinkedList() {
        // 定義內部類用於創建節點
        function Node(data) {
          this.data = data
          this.prev = null // 上一個節點引用
          this.next = null // 下一個節點引用
        }
        // 定義屬性
        this.head = null // 頭部引用
        this.tail = null // 尾部引用
        this.length = 0

        // 1.append 在尾部追加數據
        DoublyLinkedList.prototype.append = function (element) {
          var newNode = new Node(element)
          // 判斷鏈表是否爲空
          if (this.length == 0) {
            this.head = newNode
            this.tail = newNode
          } else {
            this.tail.next = newNode
            newNode.prev = this.tail
            this.tail = newNode
          }
          this.length++
        }
        // 2.正向反向遍歷
        // 2.1.forwardString 正向遍歷轉成字符串的方法
        DoublyLinkedList.prototype.forwardString = function () {
          var current = this.head
          var result = ''
          while (current) {
            result += current.data + ' '
            current = current.next
          }
          return result
        }
        // 2.2.reverseString 反向遍歷轉成字符串的方法
        DoublyLinkedList.prototype.reverseString = function () {
          var current = this.tail
          var result = ''
          while (current) {
            result += current.data + ' '
            current = current.prev
          }
          return result
        }
        // 2.3.toString 正向遍歷轉成字符串的方法
        DoublyLinkedList.prototype.toString = function () {
          return this.forwardString()
        }
        // 3.insert 在任意位置插入
        DoublyLinkedList.prototype.insert = function (position, element) {
          // 1.判斷越界問題
          if (position < 0 || position > this.length) return false
          // 2.創建新節點
          var newNode = new Node(element)
          // 3.插入元素
          if (this.length === 0) {
            this.head = newNode
            this.tail = newNode
          } else {
            if (position === 0) {
              // 插入位置爲0
              this.head.prev = newNode
              newNode.next = this.head
              this.head = newNode
            } else if (position === this.length) {
              // 插入最後的位置
              newNode.prev = this.tail
              this.tail.next = newNode
              this.tail = newNode
            } else {
              // 插入中間的位置
              var current = this.head
              var index = 0
              while (index++ < position) {
                current = current.next
              }
              newNode.next = current
              newNode.prev = current.prev
              current.prev.next = newNode
              current.prev = newNode
            }
          }
          this.length++
          return true
        }
        // 4.get 獲取指定位置數據
        DoublyLinkedList.prototype.get = function (position) {
          if (position < 0 || position >= this.length) return null
          if (this.length / 2 > position) {
            // 1.從頭部開始查找
            var current = this.head
            var index = 0
            while (index++ < position) {
              current = current.next
            }
            return current.data
          } else if (this.length / 2 <= position) {
            // 2.從末尾開始查找
            var current = this.tail
            var index = this.length - 1
            while (index-- > position) {
              current = current.prev
            }
            return current.data
          }
        }
        // 5.indexOf 返回指定數據的位置
        DoublyLinkedList.prototype.indexOf = function (data) {
          var current = this.head
          var index = 0
          while (current) {
            if (current.data == data) {
              return index
            }
            current = current.next
            index++
          }
          return -1
        }
        // 6.updete 更新指定位置的數據
        DoublyLinkedList.prototype.update = function (position, newData) {
          if (position < 0 || position >= this.length) return false
          if (this.length / 2 > position) {
            // 1.從頭開始查找
            var current = this.head
            var index = 0
            while (index++ < position) {
              current = current.next
            }
            current.data = newData
          } else {
            // 2.從尾開始查找
            var current = this.tail
            var index = this.length - 1
            while (index-- > position) {
              current = current.prev
            }
            current.data = newData
          }
          return true
        }
        // 7.removeAt 移除並其返回特定元素
        DoublyLinkedList.prototype.removeAt = function (position) {
          if (position < 0 || position >= this.length) return null

          var current = this.head
          if (this.length === 1) {
            this.head = null
            this.tail = null
          } else {
            if (position === 0) {
              this.head.next.prev = null
              this.head = this.head.next
            } else if (position === this.length - 1) {
              current = this.tail
              this.tail.prev.next = null
              this.tail = this.tail.prev
            } else {
              // var current = this.head
              var index = 0
              while (index++ < position) {
                current = current.next
              }
              current.next.prev = current.prev
              current.prev.next = current.next
            }
          }
          this.length -= 1
          return current.data
        }
        // 8.remove 移除指定的數據
        DoublyLinkedList.prototype.remove = function (data) {
          var index = this.indexOf(data)
          this.removeAt(index)
        }
        // 其他方法
        // 9.isEmpty 是否爲空
        DoublyLinkedList.prototype.isEmpty = function () {
          return this.length === 0
        }
        // 10.size 返回鏈表長度
        DoublyLinkedList.prototype.size = function () {
          return this.length
        }
        // 11.getHead 獲取鏈表第一個元素
        DoublyLinkedList.prototype.getHead = function () {
          return this.head.data
        }
        // 12.getTail 獲取鏈表最後一個元素
        DoublyLinkedList.prototype.getTail = function () {
          return this.tail.data
        }
      }
    </script>
  </body>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章