当前位置:首页 > JavaScript

js中如何实现单链表

2026-04-04 23:41:40JavaScript

单链表的基本概念

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

定义链表节点

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

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

创建链表类

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

js中如何实现单链表

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++;
}

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

js中如何实现单链表

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(' -> '));
}

完整示例

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 实现滚动的方法 使用原生 JavaScript 方法 在 Vue 中可以通过 window.scrollTo 或 Element.scrollIntoView 实现滚动。例如,滚动到页面顶部:…

h5如何实现定位

h5如何实现定位

使用HTML5 Geolocation API HTML5提供了Geolocation API,可以获取用户的地理位置信息。通过navigator.geolocation对象实现,支持获取经纬度、海拔…

vue如何实现分离

vue如何实现分离

Vue 实现代码分离的方法 Vue 提供了多种方式实现代码分离,提升项目的可维护性和模块化程度。以下是常见的几种方法: 组件化开发 将功能拆分为独立的 Vue 组件,每个组件包含自己的模板、逻辑和样…

vue如何实现高亮

vue如何实现高亮

实现文本高亮的方法 在Vue中实现文本高亮通常可以通过以下几种方式完成: 使用v-html指令结合字符串替换 通过将需要高亮的文本部分替换为HTML标签(如<span class="highl…

php 实现链表

php 实现链表

PHP 实现链表的方法 链表是一种线性数据结构,由节点组成,每个节点包含数据和指向下一个节点的指针。PHP 中可以通过类和对象来实现链表。 定义链表节点类 创建一个 ListNode 类,用于表示链…

java如何实现单点登录

java如何实现单点登录

单点登录(SSO)的基本概念 单点登录是一种用户认证机制,允许用户通过一次登录访问多个相互信任的应用系统。核心原理是通过共享认证状态(如Token或Cookie)实现跨系统身份验证。 基于Token…