双向链表结构

一 认识双向链表:

  双向链表:

        既可以从头遍历到尾, 又可以从尾遍历到头

        也就是链表相连的过程是双向的. 那么它的实现原理, 你能猜到吗?

        一个节点既有向前连接的引用, 也有一个向后连接的引用.

        双向链表可以有效的解决单向链表中提到的问题.

        双向链表有什么缺点呢?

                每次在插入或删除某个节点时, 需要处理四个节点的引用, 而不是两个. 也就是实现起来要困难一些

                并且相当于单向链表, 必然占用内存空间更大一些.

                但是这些缺点和我们使用起来的方便程度相比, 是微不足道的

双向连接的图解:

 双向链表的创建:

function woWay() {
      //内部类
      function Node(data) {
        this.data = data
        this.prev = null
        this.next = null

      }
      //属性
      this.head = null
      this.tail = null
      this.length = 0;

二  操作双向链表

    双向链表常见的操作:

  • 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():返回反向遍历的节点的字符串形式

尾部追加数据:

woWay.prototype.append = function (data) {
        //1.通过data创建一个新的节点
        let newNode = new Node(data)
        //2.判断是否添加的第一个节点
        if (this.length == 0) {//是第一个节点
          this.head = newNode

        }
        else {//2.2 不是第一个节点
          //找到最后一个节点
          let current = this.head
          while (current.next) {
            current = current.next
          }
          // 最后的next指向新的节点
          current.next = newNode
        }
        //3.length
        this.length += 1
      }

正向反向遍历

  • forwardString: 正向遍历转成字符串的方法
  • reverseString: 反向遍历转成字符串的方法
  • toString: 正向遍历转成字符串的方法
  //2.1toString
      woWay.prototype.toString = function () {
        return ''
      }
      //2.2forwardString方法
      woWay.prototype.forwardString = function () {
        //定义变量
        let current = this.tail
        let resultString = ''
        //2.依次向前进行遍历,获取每一个节点
        while (current) {
          resultString += current.data + ' '
          current = current.prev
        }
        return resultString
      }
      //2.3backwardString方法
      woWay.prototype.backwardString = function () {
        //1.定义变量
        let current = this.head
        let resultString = ''
        //2.依次向后遍历,获取每一个几点
        while (current) {
          resultString += current.data + ' '
          current = current.next
        }
        return resultString
      }

insert方法:

      woWay.prototype.insert = function (teat, data) {
        // 1.越界判断
        if (teat < 0 || teat > this.length) return false

        //2.根据我们的dtat创建新的节点
        let newNode = new Node(data)
        //3.判断我们原来的成都是否为空
        if (this.length == 0) {
          this.head = newNode
          this.tail = newNode
        }
        else {
          if (teat == 0) {//3.1判断我们的taea是否为0
            this.head.prev = newNode
            newNode.next = this.head
            this.head = newNode
          }
          else if (teat == this.length) {
            this.tail.next = newNode
            newNode.prev = this.tail
            this.tail = newNode

          }
          else {//其他情况
            let current = this.head
            let index = 0
            while (index++ < teat) {
              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方法:

      woWay.prototype.get = function (teat) {
        //1.越界判断
        if (teat < 0 || teat >= this.length) return null
        // this.length/2>teat:从头向后遍历
        // this.length/2<teat:从后向遍历

        //获取元素
        let current = this.head
        let index = 0;
        while (index++ < teat) {
          current = current.next
        }
        return current.data
      }

indexOf方法:

      woWay.prototype.indexOf = function (data) {
        //定义变量
        let current = this.head
        let index = 0
        // 2.查找和data相同的节点
        while (current) {
          if (current.data == data) {
            return index
          }
          current = current.next
          index += 1
        }
        return -1
      }

update方法:

      woWay.prototype.update = function (teat, newData) {
        // 越界判断
        if (teat < 0 || teat >= this.length) return false
        //2.寻找正确的节点
        let current = this.head
        let idnex = 0
        while (this.length / 2 < teat) {
          current = current.next
        }
        // 3.修改找到节点的data信息
        current.data = newData
        return true
      }

removeAt方法:

      woWay.prototype.removeAt = function (teat) {
        //1.越界判断
        if (teat < 0 || teat >= this.length) return null
        //2.判断是否只有一个节点
        let current = this.head
        if (this.length == 1) {
          this.head = null
          this.tail = null
        }
        else {
          if (teat == 0) {//判断删除的是否是第一个元素
            this.head.next.prev = null
            this.head = this.head.next
          }
          else if (teat == this.length - 1) {//最后的节点
            this.tail.prev.next = null
            this.tail = this.tail.prev
          }
          else {
            let index = 0
            while (index++ < teat) {
              current = current.next
            }
            current.prev.next = current.next
            current.prev.next = current.prev
          }
          //length-1
          this.length -= 1
        }
        return current.data

      }

remove方法:

      woWay.prototype.remove = function (data) {
        //1.根据data获取下标值
        let index = this.indexOf(data)
        //2.根据index删除对应位置的节点
        return this.removeAt(index)
      }

isEmpty方法:

      woWay.prototype.isEmpty = function () {
        return this.length == 0
      }

size方法:

      woWay.prototype.size = function () {
        return this.length
      }

获取链表第一个元素:

      woWay.prototype.getHead = function () {
        return this.head.data
      }

获取链表最后一个元素:

      woWay.prototype.getTail = function () {
        return this.tail.data
      }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值