数据结构之双向链表

双向链表介绍

单向链表:

  • 只能从头遍历到尾或者从尾遍历到头(一般从头到尾)
  • 也就是链表相连的过程是单向的. 实现的原理是上一个链表中有一个指向下一个的引用.

单向链表有一个比较明显的缺点:

  • 我们可以轻松的到达下一个节点, 但是回到钱一个节点是很难的. 但是, 在实际开发中, 经常会遇到需要回到上一个节点的情况
  • 举个例子: 假设一个文本编辑用链表来存储文本. 每一行用一个String对象存储在链表的一个节点中. 当编辑器用户向下移动光标时, 链表直接操作到下一个节点即可. 但是当用于将光标向上移动呢? 这个时候为了回到上一个节点, 我们可能需要从first开始, 依次走到想要的节点上.

双向链表

  • 既可以从头遍历到尾, 又可以从尾遍历到头
  • 也就是链表相连的过程是双向的. 那么它的实现原理, 你能猜到吗?
  • 一个节点既有向前连接的引用, 也有一个向后连接的引用.
  • 双向链表可以有效的解决单向链表中提到的问题.

双向链表有什么缺点呢?

  • 每次在插入或删除某个节点时, 需要处理四个节点的引用, 而不是两个. 也就是实现起来要困难一些
  • 并且相当于单向链表, 必然占用内存空间更大一些.
  • 但是这些缺点和我们使用起来的方便程度相比, 是微不足道的.

双向链表常见操作:

  • append(element):向链表尾部添加一个新的项;
  • inset(position,element):向链表的特定位置插入一个新的项;
  • get(element):获取对应位置的元素;
  • indexOf(element):返回元素在链表中的索引,如果链表中没有元素就返回-1;
  • update(position,element):修改某个位置的元素;
  • removeAt(position):从链表的特定位置移除一项;
  • isEmpty():如果链表中不包含任何元素,返回trun,如果链表长度大于0则返回false;
  • size():返回链表包含的元素个数,与数组的length属性类似;
  • toString():由于链表项使用了Node类,就需要重写继承自JavaScript对象默认的toString方法,让其只输出元素的值;
  • forwardString():返回正向遍历节点字符串形式;
  • backwordString():返回反向遍历的节点的字符串形式

双向链表的操作:

<script>
    function DoublyLinkedList() {
      //内部类:节点类
      function Node(data) {
        this.data = data
        this.prev = null
        this.next = null
      }

      //属性
      this.head = null
      this.tali = null
      this.length = 0

      //常见的操作:方法
      //1.append方法
      DoublyLinkedList.prototype.append = function (data) {
        //根据data创建节点
        var newNode = new Node(data)
        //判断添加的是否是第一个节点
        if (this.length == 0) {
          this.head = newNode
          this.tali = newNode
        } else {
          newNode.prev = this.tali
          this.tali.next = newNode
          this.tali = newNode
        }
        //length+1
        this.length += 1
      }
      //2.将链表转成字符串
      //2.1 toString方法
      DoublyLinkedList.prototype.toString = function () {
        return this.backwardString()
      }
      //2.2 forwardString方法
      DoublyLinkedList.prototype.forwardString = function () {
        //自定义变量
        var current = this.tali
        var resultString = " "
        //依次向后遍历 获取每一个节点
        while (current) {
          resultString += current.data + " "
          current = current.prev
        }
        return resultString
      }
      //2.3 backwardString方法
      DoublyLinkedList.prototype.backwardString = function () {
        //自定义变量
        var current = this.head
        var resultString = ""
        //依次向后遍历 获取每一个节点
        while (current) {
          resultString += current.data + " "
          current = current.next
        }
        return resultString
      }
      // insert方法
      DoublyLinkedList.prototype.insert = function (position, data) {
        //越界判断
        if (position < 0 || position > this.length) return false
        //根据我们的data创建新的节点
        var newNode = new Node(data)
        //判断原来的列表是否为空
        if (this.length == 0) {
          this.head = newNode
          this.tali = newNode
        } else {
          if (position == 0) {//判断position是否为0
            this.head.prev = newNode
            newNode.next = this.head
            this.head = newNode
          } else if (position == this.length) { //position==length
            newNode.prev = this.tali
            this.tali.next = newNode
            this.tali = 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
          }
        }
        //length+1
        this.length += 1
        return true
      }
      //get方法
      DoublyLinkedList.prototype.get = function (position) {
        //越界判断
        if (position < 0 || position >= this.length) return null
        //获取元素
        var current = this.head
        var index = 0
        while (index++ < position) {
          current = current.next
        }
        return current.data
      }
      //indexof测试
      DoublyLinkedList.prototype.indexOf = function (data) {
        //自定义变量
        var current = this.head
        var index = 0
        //查找data相同的节点
        while (current) {
          if (current.data == data) {
            return index
          }
          current = current.next
          index += 1
        }
        return -1
      }
      //update方法
      DoublyLinkedList.prototype.update = function (position, newData) {
        //越界判断
        if (position < 0 || position >= this.length) return false
        //寻找正确的节点
        var current = this.head
        var index = 0
        while (index++ < position) {
          current = current.next
        }
        //修改找到节点data的信息
        current.data = newData

        return true
      }
      //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.tali = null
        } else {
          if (position == 0) { //判断是否删除的是第一个节点
            this.head.next.prev = null
            this.head = this.head.next
          } else if (position == this.length - 1) { //最后节点
            current = this.tali
            this.tali.prev.next = null
            this.tali = this.tali.prev
          } else {
            var index = 0
            var current = this.head

            while (index++ < position) {
              current = current.next
            }
            current.prev.next = current.next
            current.next.prev = current.prev
          }
        }
        //length-1
        this.length -= 1

        return current.data
      }

      //remove方法
      DoublyLinkedList.prototype.remove = function (data) {
        //根据data获取下标值
        var index = this.indexOf(data)
        //根据index删除对应位置的节点
        return this.removeAt(index)
      }
      //isEmpty方法
      DoublyLinkedList.prototype.isEmpty = function () {
        return this.length == 0
      }
      //size方法
      DoublyLinkedList.prototype.size = function () {
        return this.length
      }

    }
    //测试代码
    var list = new DoublyLinkedList()
    //测试append方法
    list.append('aaa')
    list.append('bbb')
    list.append('ccc')

    //测试转成字符串方法
    // alert(list)
    // alert(list.backwardString())
    // alert(list.forwardString())
    //测试插入insert
    list.insert(0, 'nba')
    list.insert(4, 'cba')
    list.insert(2, 'cuba')
    alert(list)
    //测试get
    // alert(list.get(0))
    // alert(list.get(2))
    //测试indexOf
    alert(list.indexOf('aaa'))
    alert(list.indexOf('bbb'))
    //测试update方法
    list.update(0, 'mmm')
    alert(list)
    //测试removeAt
    list.removeAt(0)
    alert(list)
    //测试remove
    list.remove('bbb')
    alert(list)
    list.remove('ccc')
    alert(list)

    //isEmpty
    alert(list.isEmpty())
    //size
    alert(list.size())
  </script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值