JS实现双向链表

在这里插入图片描述
优势:
在这里插入图片描述
链表实现:
在这里插入图片描述

第一步:实现节点

在这里插入图片描述

function Node(key, value) {
  this.key = key;//存放key
  this.value = value;//存放value
  this.prev = null;//指向前边节点
  this.next = null;//指向后边节点

  //get方法 获取node的值
  this.get = function () {
    return `{${this.key}: ${this.value}}`
  }
}

第二步:实现双向链表
在这里插入图片描述

function DoubleLinkList() {
  this.head = null;//存放链表头结点
  this.tail = null;//存放链表尾节点
  this.size = 0;//链表当前节点数
	
  //头部添加节点
  this.addHead = function (node) {
    if (!this.head) {
      //当链表头部无节点时表示链表为空
      this.head = node;
      this.tail = node;
      this.head.next = null;
      this.head.prev = null;
    } else {
      //1.将节点的next指向头部节点 2.将头部节点的prev指向node 
      //3.将头部节点赋值成node 4.将头部节点prev设为null
      node.next = this.head;
      this.head.prev = node;
      this.head = node;
      this.head.prev = null;
    }
    this.size += 1
    return node
  }
  //添加尾部节点
  this.addTail = function (node) {
    if (!this.tail) {
      //当链表头部无节点时表示链表为空
      this.head = node;
      this.tail = node;
      this.tail.next = null;
      this.tail.prev = null;
    } else {
      //1.将节点的prev 指向尾部节点 2.将尾部节点的next 指向node 
      //3.将尾部节点赋值成node 4.将尾部节点next设为null
      node.prev = this.tail;
      this.tail.next = node;
      this.tail = node;
      this.tail.next = null;
    }
    this.size += 1
    return node
  }
  //删除头部节点
  this.delHead = function () {
    //判断有无头部结点
    if (!this.head) return
    let node = this.head
    //判断头部节点有无next
    if (node.next) {
      this.head = node.next
      this.head.prev = null;
    } else {
      this.head = null;
      this.tail = null
    }
    this.size -= 1
    return node
  }
  //删除尾部节点
  this.delTail = function () {
    //判断有无尾部节点
    if (!this.tail) return
    let node = this.tail
    //判断尾部节点有无prev
    if (node.prev) {
      this.tail = node.prev
      this.tail.next = null;
    } else {
      this.tail = null;
      this.head = null
    }
    this.size -= 1
    return node
  }
  //删除任意节点
  this._remove = function (node) {
    //不传node删除尾部节点
    if (!node) node = this.tail
    if (node == this.tail) {
      this.delTail()
    } else if (node == this.head) {
      this.delHead()
    } else {
      node.prev.next = node.next;
      node.next.prev = node.prev;
      this.size -= 1
    }
    return node
  }
  this.pop = function () {
    return this.delHead()
  }
  this.append = function (node) {
    return this.addTail(node)
  }
  this.appendFront = function (node) {
    return this.addHead(node)
  }
  this.remove = function (node = null) {
    return this._remove(node)
  }
  this.print = function () {
    let node = this.head
    let str = ''
    while (node) {
      str += node.get()
      node = node.next
      if (node) str += '=>'
    }
    console.log(str)
  }
}

测试:

let l = new DoubleLinkList()
nodes = []
for (let i = 0; i < 10; i++) {
  let node = new Node(i, i)
  nodes.push(node)
}

l.addHead(nodes[0])
l.print()
l.append(nodes[1])
l.print()
l.pop()
l.print()
l.append(nodes[2])
l.print()
l.appendFront(nodes[3])
l.print()
l.append(nodes[4])
l.print()
l.remove(nodes[2])
l.print()
l.remove()
l.print()

输出:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值