链表

 

链表由一系列节点组成的集合。每个节点都使用一个对象的引用指向当前节点的后继。链表的尾元素指向一个null节点。

 单项链表

append 尾部追加元素
insert  中间插入一个元素  positon是索引 从0开始 postion=0 表示第一个元素 false 插入失败
get 获取对应位置的节点 获取不到返回null
indexOf  返回节点在链表中的索引,如果元素不存在返回-1
update  修改某个位置的元素 修改成功返回ture 修改失败返回false
removeAt  从链表中的特定位置移除一项 返回被删除的元素 反之返回null
remove  从链表中移除一项 移除成功返回true 反之返回false
null
class Node {
  constructor(item) {
    this.item = item;
    this.next = null
  }
}
class linkedList {
  constructor() {
    this.head = null
    this.length = 0
  }
  //尾部追加一个元素
  append(item) {
    const node = new Node(item)
    let current = this.head
    let previous = null
    if (!current) this.head = node
    else {
      while (current) {
        previous = current //保存上一个节点
        current = current.next
      }
      previous.next = node
    }
    this.length++
  }
  //中间插入一个元素  positon是索引 从0开始 postion=0 表示第一个元素 false 插入失败
  insert(position, item) {
    if (position < 0 || position > this.length) return false;
    const node = new Node(item)
    let previous = null;
    let current = this.head
    let index = 0
    if (!current) {
      this.head.next = node
    } else {
      while (current) {
        if (index === position) break
        previous = current
        current = current.next
        index++

      }
      previous.next = node
      node.next = current

    }
    this.length++
    return true

  }
  //获取对应位置的节点
  get(position) {
    if (position < 0 || position >= this.length) return null
    let current = this.head
    if (!current) return null
    let index = 0
    while (current) {
      if (index === position) return current.item
      current = current.next
      index++
    }

  }
  //返回节点在链表中的索引,如果元素不存在返回-1
  indexOf(item) {
    let current = this.head
    let index = 0
    if (!current) return -1
    while (current) {
      if (current.item === item) return index
      current = current.next
      index++
    }
    return -1
  }
  //修改某个位置的元素 修改成功返回ture 修改失败返回false
  update(position, item) {
    if (position < 0 || position >= item.length) return false
    let current = this.head
    let index = 0
    if (!current) return false
    while (current) {
      if (index === position) break
      current = current.next
      index++
    }
    current.item = item
    return true
  }
  //从链表中的特定位置移除一项 返回被删除的元素
  removeAt(position) {
    if (position < 0 || position >= this.length) return null;
    let current = this.head
    if (!current) return null
    let previous = null
    let index = 0
    if (position === 0) {
      this.head = current.next
      this.length--
      return current.item
    }
    while (current) {
      if (index === position) break
      previous = current
      current = current.next
      index++
    }
    previous.next = current.next
    this.length--
    return current.item
  }
  //从链表中移除一项 移除成功返回true 反之返回false
  remove(item) {
    let current = this.head
    let previous = null
    if (!current) return false
    if (current.item === item) {
      this.head = current.next
      this.length--
      return true

    } else {
      while (current) {
        if (current.item === item) break
        previous = current
        current = current.next
      }
      if (!current) return false //意思就是说:如果便利整个链表都没找到item 说明item不在链表中 返回false
      previous.next = current.next
      this.length--
      return true

    }

  }

  //打印
  toString() {
    let current = this.head
    const arr = []
    if (!current) return null
    while (current) {
      const item = current.item
      arr.push(item)
      current = current.next
    }
    return arr.join(',')
  }
}
const link = new linkedList()
link.append('a')
link.append('b')
link.append('c')
View Code

 

posted on 2020-10-20 12:27  章画  阅读(68)  评论(0编辑  收藏  举报

导航