当前位置:首页 > JavaScript

js中如何实现单链表

2026-01-31 07:41:31JavaScript

单链表的基本概念

单链表是一种线性数据结构,由一系列节点组成,每个节点包含数据和指向下一个节点的指针。链表的头节点是访问整个链表的起点,尾节点的指针通常指向null

js中如何实现单链表

单链表的节点定义

在JavaScript中,可以通过类或构造函数定义链表节点。每个节点包含value(存储数据)和next(指向下一个节点的指针)。

js中如何实现单链表

class ListNode {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

单链表的实现

以下是单链表的完整实现,包含插入、删除、查找等基本操作。

class LinkedList {
  constructor() {
    this.head = null;
    this.size = 0;
  }

  // 在链表尾部添加节点
  append(value) {
    const newNode = new ListNode(value);
    if (!this.head) {
      this.head = newNode;
    } else {
      let current = this.head;
      while (current.next) {
        current = current.next;
      }
      current.next = newNode;
    }
    this.size++;
  }

  // 在链表头部添加节点
  prepend(value) {
    const newNode = new ListNode(value);
    newNode.next = this.head;
    this.head = newNode;
    this.size++;
  }

  // 在指定位置插入节点
  insertAt(value, index) {
    if (index < 0 || index > this.size) return false;
    if (index === 0) {
      this.prepend(value);
      return true;
    }
    const newNode = new ListNode(value);
    let current = this.head;
    let previous = null;
    let i = 0;
    while (i < index) {
      previous = current;
      current = current.next;
      i++;
    }
    newNode.next = current;
    previous.next = newNode;
    this.size++;
    return true;
  }

  // 删除指定位置的节点
  removeAt(index) {
    if (index < 0 || index >= this.size) return null;
    let current = this.head;
    if (index === 0) {
      this.head = current.next;
    } else {
      let previous = null;
      let i = 0;
      while (i < index) {
        previous = current;
        current = current.next;
        i++;
      }
      previous.next = current.next;
    }
    this.size--;
    return current.value;
  }

  // 查找节点值是否存在
  contains(value) {
    let current = this.head;
    while (current) {
      if (current.value === value) return true;
      current = current.next;
    }
    return false;
  }

  // 获取链表长度
  getSize() {
    return this.size;
  }

  // 打印链表
  print() {
    let current = this.head;
    const values = [];
    while (current) {
      values.push(current.value);
      current = current.next;
    }
    console.log(values.join(' -> '));
  }
}

使用示例

以下是单链表的简单使用示例:

const list = new LinkedList();
list.append(10); // 链表: 10
list.append(20); // 链表: 10 -> 20
list.prepend(5); // 链表: 5 -> 10 -> 20
list.insertAt(15, 2); // 链表: 5 -> 10 -> 15 -> 20
list.removeAt(1); // 链表: 5 -> 15 -> 20
console.log(list.contains(15)); // true
console.log(list.getSize()); // 3
list.print(); // 5 -> 15 -> 20

时间复杂度分析

  • 插入操作:头部插入(prepend)为O(1),尾部插入(append)和指定位置插入(insertAt)为O(n)。
  • 删除操作:头部删除为O(1),其他位置删除为O(n)。
  • 查找操作contains和按索引访问均为O(n)。

分享给朋友:

相关文章

php如何实现直播

php如何实现直播

实现直播功能的方法 PHP可以通过结合其他技术和工具来实现直播功能。以下是几种常见的方法: 使用流媒体服务器 配置流媒体服务器如Nginx-RTMP、Red5或Wowza。这些服务器支持RTMP协…

js双击事件如何实现

js双击事件如何实现

实现双击事件的方法 在JavaScript中,可以通过监听dblclick事件或手动检测两次点击的时间间隔来实现双击事件。以下是几种常见的方法: 使用原生dblclick事件 element…

vue如何实现计算

vue如何实现计算

Vue 计算属性的实现方法 在 Vue 中,计算属性(computed properties)是一种基于响应式依赖进行缓存的属性,适用于处理复杂逻辑或依赖其他数据属性的场景。以下是实现计算属性的几种方…

如何实现语音react

如何实现语音react

语音识别基础设置 在React中实现语音识别功能通常需要借助浏览器的Web Speech API或第三方库。Web Speech API提供了SpeechRecognition接口,允许应用程序直接访…

链表实现js

链表实现js

链表的基本概念 链表是一种线性数据结构,由一系列节点组成,每个节点包含数据和一个指向下一个节点的指针(单向链表)或两个指针(双向链表)。与数组不同,链表在内存中非连续存储,插入和删除操作更高效,但随机…

vue如何实现递归

vue如何实现递归

递归组件的实现方法 在Vue中实现递归组件通常用于渲染树形结构或嵌套数据。核心思路是组件在其模板中调用自身,但需注意终止条件以避免无限循环。 定义递归组件 组件需设置name选项,才能在模板中调用自…