javascript链表实现

function LinkedList() {
  // 内部类
  function Node(data) {
    this.data = data
    this.next = null
  }
  // 属性
  this.head = null
  this.length = 0
  // 列表尾部添加
  LinkedList.prototype.append = function(element) {
    let newNode = new Node(element)
    if(this.length === 0) {
      this.head = newNode
    } else {
      let current = this.head
      while(current.next) {
        current = current.next
      }
      current.next = newNode
    }
    this.length += 1
  }
  // 任意位置添加
  LinkedList.prototype.insert = function(position, element) {
    if(position < 0 || position > this.length) {
      return false
    }
    let newNode = new Node(element)
    if(position === 0) {
      newNode.next = this.head
      this.head = newNode
    } else {
      let index = 0
      let current = this.head
      while(index++ < position - 1){
        current = current.next
      }
      newNode.next = current.next
      current.next = newNode
    }
    this.length += 1
  }
  // 获取对应位置的元素
  LinkedList.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
  }
  // 返回元素在列表中的索引,如果没有则返回-1
  LinkedList.prototype.indexOf = function(element) {
    let current = this.head
    let index = 0
    while(current.next) {
      if(element === current.data) {
        return index
      } else {
        index += 1
        current = current.next
      }
    }
    if(current.data === element) {
      return this.length - 1
    }
    return -1
  }
  // 修改某元素
  LinkedList.prototype.update = function(position, element) {
    if(position < 0 || position >= this.length) {
      return false
    }
    let index = 0
    let current = this.head
    while(index++ < position){
      current = current.next
    }
    current.data = element
  }
  // 从特定位置移除一项
  LinkedList.prototype.removeAt = function(position) {
    if(position < 0 || position >= this.length) {
      return null
    }
    let index = 0
    let current = this.head
    if(position === 0) {
      this.head = current.next
    } else {
      let previous = null
      while(index++ < position){
        previous = current
        current = current.next
      }
      previous.next = current.next
    }
    this.length -= 1
    return current.data
  }
  // 从列表中移除一项
  LinkedList.prototype.remove = function(element) {
    let position = this.indexOf(element)
    if(position < 0) return undefined
    return this.removeAt(position)
  }
  LinkedList.prototype.isEmpty = function() {
    return this.head === null
  }
  LinkedList.prototype.size = function() {
    return this.length
  }
  LinkedList.prototype.toString = function() {
    let current = this.head
    let listString = ''
    while(current) {
      listString += current.data + ' '
      current = current.next
    }
    return listString
  }
}

let linkList = new LinkedList()
linkList.append('a')
linkList.append('b')
linkList.append('c')
linkList.append('d')
console.log(linkList.toString());
linkList.insert(2,'1')
console.log(linkList.toString());
console.log(linkList.get(0));
console.log(linkList.indexOf('x'));
linkList.update(5,'b');
console.log(linkList.toString());
console.log(linkList.removeAt(3));
console.log(linkList.toString());
console.log(linkList.remove('m'));
console.log(linkList.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、付费专栏及课程。

余额充值