javascript实现双向链表

function DoublyLinkedList() {
  this.head = null
  this.tail = null
  this.length = 0
  function Node(data) {
    this.data = data
    this.next = null
    this.prev = null
  }
  DoublyLinkedList.prototype.append = function(el) {
    let newNode = new Node(el)
    if(this.length === 0) {
      this.head = newNode
      this.tail = newNode
    } else {
      this.tail.next = newNode
      newNode.prev = this.tail
      this.tail = newNode
    }
    this.length += 1
  }
  DoublyLinkedList.prototype.insert = function(position, el) {
    if(position<0 || position > this.length) return false
    let newNode = new Node(el)
    if(position === 0) {
      let current = this.head
      this.head = newNode
      current.prev = newNode
      newNode.next = current
    } else {
      let index = 0
      let current = this.head
      while(index++ < position - 1) {
        current = current.next
      }
      if(!current.next) {
        newNode.prev = current
        current.next = newNode
        this.tail = newNode
      } else {
        newNode.next = current.next
        newNode.prev = current
        current.next.prev = newNode
        current.next = newNode
      }
      }
      this.length += 1
    }
  DoublyLinkedList.prototype.get = function(position) {
    if(position < 0 || position >= this.length) return false
    let index = 0
    let current = this.head
    while(index++ < position) {
      current = current.next
    }
    return current.data
  }
  DoublyLinkedList.prototype.indexOf = function(el) {
    let current = this.head
    let index = 0
    while(current) {
      if(current.data === el) {
        return index
      }
      current = current.next
      index += 1
    }
    if(current.data == el) return this.length - 1
    return -1
  }
  DoublyLinkedList.prototype.update = function(position, el) {
    if(position < 0 || position >= this.length) return false
    let index = 0
    let current = this.head
    while(index++ < position) {
      current = current.next
    }
    current.data = el
  }
  DoublyLinkedList.prototype.removeAt = function(position) {
    if(position < 0 || position >= this.length) return false
    let current = this.head
    if(position === 0) {
      this.head = current.next
      current.next.prev = null
    }else{
      let index = 0
      while(index++ < position) {
        current = current.next
      }
      if(!current.next) {
        this.tail = current.prev
        current.prev.next = null
      } else{
        current.prev.next = current.next
        current.next.prev = current.prev
      }
    }
    return current
  }
  DoublyLinkedList.prototype.remove = function(element) {
    let index = this.indexOf(element)
    if(index < 0) {
      return undefined
    }
    return this.removeAt(index)
  }
  DoublyLinkedList.prototype.isEmpty = function() {
    return this.length === 0
  }
  DoublyLinkedList.prototype.size = function() {
    return this.length
  }
  DoublyLinkedList.prototype.toString = function() {
    let current = this.head
    let resultString = ''
    while(current) {
      resultString += current.data + ' '
      current = current.next
    }
    return resultString
  }
  // 向前遍历
  DoublyLinkedList.prototype.forwardString = function() {
    let current = this.tail
    let resultString = ''
    while(current) {
      resultString += current.data + ' '
      current = current.prev
    }
    return resultString
  }
  // 向后遍历
  DoublyLinkedList.prototype.backwardString = function() {
    let current = this.head
    let resultString = ''
    while(current) {
      resultString += current.data + ' '
      current = current.next
    }
    return resultString
  }
}

let dLinkedList = new DoublyLinkedList()
dLinkedList.append('a')
dLinkedList.append('b')
dLinkedList.append('c')
dLinkedList.append('d')
dLinkedList.append('e')
dLinkedList.append('f')
dLinkedList.insert(2,'2')
console.log(dLinkedList.toString());
console.log(dLinkedList.forwardString());
console.log(dLinkedList.get(3));
console.log(dLinkedList.indexOf('a'));
dLinkedList.update(3,'3')
console.log(dLinkedList.toString());
dLinkedList.removeAt(6)
console.log(dLinkedList.toString());
dLinkedList.remove('e')
console.log(dLinkedList.toString());

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今天长高了没1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值