当前位置:首页 > JavaScript

js中如何实现单链表

2026-04-04 23:41:40JavaScript

单链表的基本概念

单链表是一种线性数据结构,由一系列节点组成,每个节点包含数据和指向下一个节点的指针。最后一个节点的指针指向null,表示链表结束。

定义链表节点

使用class或构造函数定义节点,每个节点包含value(数据)和next(指向下一个节点的指针)。

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

创建链表类

定义一个链表类,包含头节点(head)和基础操作方法。

class LinkedList {
  constructor() {
    this.head = null;
    this.size = 0; // 可选:记录链表长度
  }
}

插入节点

头部插入:将新节点作为链表的头节点。

insertFirst(value) {
  const newNode = new Node(value);
  newNode.next = this.head;
  this.head = newNode;
  this.size++;
}

尾部插入:遍历链表找到最后一个节点,将新节点链接到末尾。

insertLast(value) {
  const newNode = new Node(value);
  if (!this.head) {
    this.head = newNode;
  } else {
    let current = this.head;
    while (current.next) {
      current = current.next;
    }
    current.next = newNode;
  }
  this.size++;
}

删除节点

删除指定值节点:遍历链表找到目标节点,调整指针跳过该节点。

delete(value) {
  if (!this.head) return;
  if (this.head.value === value) {
    this.head = this.head.next;
    this.size--;
    return;
  }
  let current = this.head;
  while (current.next) {
    if (current.next.value === value) {
      current.next = current.next.next;
      this.size--;
      return;
    }
    current = current.next;
  }
}

查找节点

遍历链表,检查是否存在匹配的值。

contains(value) {
  let current = this.head;
  while (current) {
    if (current.value === value) return true;
    current = current.next;
  }
  return false;
}

打印链表

将链表的值转换为数组或字符串输出。

js中如何实现单链表

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

完整示例

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

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

  insertFirst(value) {
    const newNode = new Node(value);
    newNode.next = this.head;
    this.head = newNode;
    this.size++;
  }

  insertLast(value) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
    } else {
      let current = this.head;
      while (current.next) {
        current = current.next;
      }
      current.next = newNode;
    }
    this.size++;
  }

  delete(value) {
    if (!this.head) return;
    if (this.head.value === value) {
      this.head = this.head.next;
      this.size--;
      return;
    }
    let current = this.head;
    while (current.next) {
      if (current.next.value === value) {
        current.next = current.next.next;
        this.size--;
        return;
      }
      current = current.next;
    }
  }

  contains(value) {
    let current = this.head;
    while (current) {
      if (current.value === value) return true;
      current = current.next;
    }
    return false;
  }

  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.insertLast(1);
list.insertLast(2);
list.insertFirst(0);
list.print(); // 输出: 0 -> 1 -> 2
list.delete(1);
list.print(); // 输出: 0 -> 2

注意事项

  • 插入和删除操作需处理边界条件(如空链表或头节点)。
  • 链表操作的时间复杂度为O(n),因需遍历节点。
  • 可根据需求扩展方法(如插入到指定位置、反转链表等)。

分享给朋友:

相关文章

vue如何实现登录

vue如何实现登录

Vue 实现登录功能的方法 创建登录表单组件 在 Vue 项目中创建一个登录表单组件,通常命名为 Login.vue。表单包含用户名和密码输入框,以及提交按钮。 <template>…

java如何实现多线程

java如何实现多线程

实现多线程的方法 在Java中,实现多线程主要有两种方式:继承Thread类和实现Runnable接口。此外,还可以使用ExecutorService等高级工具类来管理线程。 继承Thread类 通…

vue router如何实现

vue router如何实现

Vue Router 的实现方法 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是实现 Vue Router 的具体方法: 安装 Vue Router…

java如何实现分布式

java如何实现分布式

分布式系统实现方式 Java实现分布式系统通常涉及多种技术和框架,以下是几种核心方法: 基于RPC框架 使用Dubbo、gRPC等RPC框架实现服务间的远程调用。Dubbo提供高性能的RPC通信,支…

VUE如何实现长按

VUE如何实现长按

VUE 实现长按功能的方法 在Vue中实现长按功能可以通过监听触摸或鼠标事件来实现。以下是几种常见的方法: 方法一:使用原生事件监听 通过监听 touchstart 和 touchend 或 mo…

问卷vue如何实现

问卷vue如何实现

在Vue中实现问卷功能 使用Vue框架可以高效地构建动态问卷系统,主要依赖组件化开发和响应式数据绑定。以下是具体实现方法: 基础结构搭建 创建Vue项目后,设计问卷的数据结构。通常使用一个数组存储问…