数据结构-链表(二)

103 阅读1分钟

今天学习的是双向链表,与单向链表相比,双向链表的尾部多了一个tail指针指向尾部最后一个元素,并且每一个元素都有一个prev指针指向上一个元素节点。头部元素的prev指向null,尾部元素的next指向null,大致的图示如图: image.png

双向链表实现过程:
    class DoublyLinkedList {
      constructor() {
        this.head = null
        this.tail = null
        this.length = 0
      }
      // 追加元素
      append(element) {
        // 创建新节点
        const newNode = {
          data: element,
          next: null,
          prev: null
        }
        // 判断链表是否为空
        if (this.length === 0) {
          this.head = newNode
          this.tail = newNode
        } else {
          newNode.prev = this.tail
          this.tail.next = newNode
          this.tail = newNode
        }
        this.length++
      }
      // 特定位置插入新的元素
      insert(position, element) {
        //  边界判断position
        if (position < 0 || position > this.length) return false
        // 创建新节点
        const newNode = {
          data: element,
          next: null,
          prev: null
        }
        // 插入数据
        if (this.length === 0) {
          this.head = newNode
          this.tail = newNode
        } else {
          // 插入到顶部
          if (position === 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 {
            // 插入到中间
            let currentNode = this.head
            let index = 0
            while (index < position) {
              currentNode = currentNode.next
              index++
            }
            //修改指针
            newNode.prev = currentNode.prev
            newNode.next = currentNode
            currentNode.prev.next = newNode
            currentNode.prev = newNode
          }
        }
        this.length++
      }
      // 获取特定位置的元素
      get(position) {
        // 边界判断
        if (position < 0 || position >= this.length) return null
        // 二分法
        if (this.length / 2 < position) {
          // 从头部开始数
          let currentNode = this.tail
          let index = this.length - 1
          while (index > position) {
            currentNode = currentNode.prev
            index--
          }
          return currentNode.data
        } else {
          // 从尾部开始数
          let currentNode = this.head
          let index = 0
          while (index < position) {
            currentNode = currentNode.next
            index++
          }
          return currentNode.data
        }
      }
      // 获取特定元素的位置
      indexOf(element) {
        let currentNode = this.head
        let index = 0
        while (currentNode) {
          if (currentNode.data === element) return index
          currentNode = currentNode.next
          index++
        }
        return -1
      }
      // 更新特定位置的元素
      update(position, element) {
        // 边界判断
        if (position < 0 || position >= this.length) return false
        // 二分法
        if (this.length / 2 < position) {
          // 从头部开始数
          let currentNode = this.tail
          let index = this.length - 1
          while (index++ > position) {
            currentNode = currentNode.prev
          }
          currentNode.data = element
        } else {
          // 从尾部开始数
          let currentNode = this.head
          let index = 0
          while (index++ < position) {
            currentNode = currentNode.next
          }
          currentNode.data = element
        }
        return true
      }
      // 删除特定位置的元素
      removeAt(position) {
        // 边界判断
        if (position < 0 || position >= this.length) return null
        let currentNode = this.head
        // 判读是否只有一个元素
        if (this.length === 1) {
          this.head = null
          this.tail = null
        } else {
          // 判断是否删除的是第一个元素
          if (position === 0) {
            this.head = this.head.next
            this.head.prev = null
          } else if (position === this.length - 1) {
            // 判断是否删除的是最后一个元素
            currentNode = this.tail
            this.tail = this.tail.prev
            this.tail.next = null
          } else {
            // 删除中间的元素

            let index = 0
            while (index++ < position) {
              currentNode = currentNode.next
            }
            currentNode.prev.next = currentNode.next
            currentNode.next.prev = currentNode.prev
          }
        }
        this.length--
        return currentNode.data
      }
      // 删除特定元素
      remove(element) {
        let index = this.indexOf(element)
        return this.removeAt(index)
      }
      // 返回第一个元素
      getHead() {
        return this.head
      }
      // 返回最后一个元素
      getTail() {
        return this.tail
      }
}

双向链表的一个实际应用是大多数的文本编辑器的光标选中文字,上下换行等,文字的每一行可以看作是一个节点。