链表:双向链表

即可以从头遍历到尾,又可以从尾遍历到头。

也就是链表相连的过程是双向的,一个节点既有向前连接的引用,也有一个向后连接的引用。

缺点:

        每次插入或删除某个节点时,需要处理4个引用,而不是2个

        当于单向链表,必然占用内存空间更大

这个时候,node1的prev等于null,node1的next等于node2,node2的prev等于node1,node2的next等于node3,node3的prev等于node2,node3的next等于null

node1.prev=null   node1.next = node2  

node2.prev=node1 node2.next = node3

node3.prev = node2  node3.next = null

  

function DoubleLinkeList() {

    function Node(data) {

      this.data = data;

      this.prev = null;

      this.next = null;

    }

    // 包含属性

    this.head = null;

    this.tail = null;//上一个节点

    this.length = 0;

    DoubleLinkeList.prototype.oppend = (data) => {

      var newNode = new Node(data)

      // 判断是否添加的节点是第一个节点

      if (this.length == 0) {

        this.tail = newNode;

        this.head = newNode

      } else {

        newNode.prev = this.tail

        this.tail.next = newNode

        this.tail = newNode

      }

      this.length += 1

    }

    DoubleLinkeList.prototype.toString = () => {

      return this.backWardString()

    }

    // 向后

    DoubleLinkeList.prototype.backWardString = () => {

      var current = this.head

      var currentString = ''

      while (current) {

        currentString += current.data

        current = current.next

      }

      return currentString

    }

    // 向前

    DoubleLinkeList.prototype.BeforeWardString = () => {

      var current = this.tail;

      var currentString = ''

      while (current) {

        currentString += current.data

        current = current.prev

      }

      return currentString

    }

    // 插入

    DoubleLinkeList.prototype.insert = (position, data) => {

      // 首先进行越界判断

      if (position < 0 || position > this.length) return null;

      var newNode = new Node(data)

      //判断是否为空链表

      if (this.length == 0) {

        this.tail = newNode;

        this.head = 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 {

          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 += 1

    }

  }

  var doubleLinke = new DoubleLinkeList()

  doubleLinke.oppend(123)

  doubleLinke.oppend(456)

  doubleLinke.oppend(789)

  doubleLinke.insert(1,666)

  console.log(doubleLinke.toString())

        

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值