数据结构和算法

数据结构 — 链表

2022-05-28  本文已影响0人  lio_zero

链表(Linked list)是一种表示元素集合的线性数据结构,其中每个元素指向下一个元素。链表中的第一个元素是 head,最后一个元素是 tail。

JavaScript 链表可视化

链表数据结构的每个元素必须具有以下属性:

链表数据结构的主要属性有:

链表数据结构的主要操作有:

JavsScript 实现

class LinkedList {
  constructor() {
    this.nodes = []
  }

  get size() {
    return this.nodes.length
  }

  get head() {
    return this.size ? this.nodes[0] : null
  }

  get tail() {
    return this.size ? this.nodes[this.size - 1] : null
  }

  insertAt(index, value) {
    const previousNode = this.nodes[index - 1] || null
    const nextNode = this.nodes[index] || null
    const node = { value, next: nextNode }

    if (previousNode) previousNode.next = node
    this.nodes.splice(index, 0, node)
  }

  insertFirst(value) {
    this.insertAt(0, value)
  }

  insertLast(value) {
    this.insertAt(this.size, value)
  }

  getAt(index) {
    return this.nodes[index]
  }

  removeAt(index) {
    const previousNode = this.nodes[index - 1]
    const nextNode = this.nodes[index + 1] || null

    if (previousNode) previousNode.next = nextNode

    return this.nodes.splice(index, 1)
  }

  clear() {
    this.nodes = []
  }

  reverse() {
    this.nodes = this.nodes.reduce(
      (acc, { value }) => [{ value, next: acc[0] || null }, ...acc],
      []
    )
  }

  *[Symbol.iterator]() {
    yield* this.nodes
  }
}
const list = new LinkedList()

list.insertFirst(1)
list.insertFirst(2)
list.insertFirst(3)
list.insertLast(4)
list.insertAt(3, 5)

list.size // 5
list.head.value // 3
list.head.next.value // 2
list.tail.value // 4
;[...list.map((e) => e.value)] // [3, 2, 1, 5, 4]

list.removeAt(1) // 2
list.getAt(1).value // 1
list.head.next.value // 1
;[...list].map((e) => e.value) // [3, 1, 5, 4]

list.reverse()
;[...list].map((e) => e.value) // [4, 5, 1, 3]

list.clear()
list.size // 0

以上内容译自 30 seconds of code 的 JavaScript Data Structures - Linked List

Leetcode 相关的链表题目

更多资料

Linked List

上一篇 下一篇

猜你喜欢

热点阅读