JavaScript数据结构--链表

139 阅读2分钟

链表是一种数据结构。

实现一个单项链表

// 链表单个节点
class Node {
  constructor (value) {
    this.value = value
    this.next = null
  }
}

实现一个链表的

append 添加节点方法

removeAt 删除指定位置节点

insert 指定位置插入

indexOf 查找是否包含

remove 删除指定节点

size 返回链表代下

isEmpty 判断是否为空

初始化方法如下

class LinkedList {
  constructor (value = null) {
    this.head = null
    this.length = 0
    if (value) {
      this.head = new Node(value)
      this.length = 1
    }
  }
  append (value) {}
  removeAt (position) {}
  insert (position, value) {}
  indexOf (value, start = 0) {}
  remove (value, start = 0) {}
  size () {}
  isEmpty () {}
}

完整代码

// 链表单个节点
class Node {
  constructor (value) {
    this.value = value
    this.next = null
  }
}

class LinkedList {
  constructor (value = null) {
    this.head = null
    this.length = 0
    if (value) {
      this.head = new Node(value)
      this.length = 1
    }
  }
  
  append (value) {
    const node = new Node(value)
    
    // 如果head为null 说明链表没有数据,直接添加到头部
    if (this.head === null) {
      this.head = node
    } else {
      let current = this.head
      
      // 循环最next为false说明next为null,则把当前节点添加到链表尾部
      while (current.next) {
        current = current.next
      }
      current.next = node
    }
    this.length += 1
  }
  
  removeAt (position) {
    
    // 判断移除的节点是否在 链表内
    if (position >= this.length || position < 0) {
      return null
    }
    
    let current = this.head
    
    // 如果为0,则直接将 head 设置为 head.next
    if (position === 0) {
      this.head = current.next
    } else {
      let index = 0
      let prev = null
      
      // 循环 找到需要删除position的节点,将其上一个节点的next 设置为当前的节点
      while (index < position) {
        prev = current
        current = current.next
        index += 1
      }
      prev.next = current.next
    }
    
    this.length -= 1
    return current.next
  }
  
  insert (position, value) {
    if (position >= this.length || position < 0) {
      return false
    }
    
    const node = new Node(value)
    
    // 节点在头部则 直接将节点next指向this.head。然后将head的指针指向新节点
    if (position === 0) {
      node.next = this.head
      this.head = node
    } else {
      let index = 0
      let current = this.head
      let prev = null
      
      // 遍历循环找节点添加进入
      while (index < position) {
        prev = current
        current = current.next
        index += 1
      }
      node.next = current
      prev.next = node
    }
    this.length += 1
    return true
  }
  
  indexOf (value, start = 0) {
    if (start > this.length) {
      return false
    }
    
    let index = 0
    let current = this.head
    
    while (index < this.length) {
      if (current.value === value && start <= index) {
        return index
      }
      current = current.next
      index += 1
    }
    return -1
  }
  
  remove (value, start = 0) {
    const index = this.indexOf(value, start)
    return this.removeAt(index)
  }
  
  size () {
    return this.length
  }
  
  isEmpty () {
    return !!this.length
  }
}

let test = new LinkedList('a')
test.append('b')
test.append('c')
test.removeAt('1')
test.insert(1, 'e')
let e = test.indexOf('e')
console.log('e', e)
console.log('test', test)