当前位置:首页 > JavaScript

js中如何实现单链表

2026-03-01 22:52:25JavaScript

单链表的基本概念

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

定义节点类

使用class或构造函数定义链表节点,每个节点包含value(存储数据)和next(指向下一个节点)属性。

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

创建链表实例

初始化链表时,通常需要定义头节点(head)和尾节点(tail),初始时链表为空。

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

实现插入操作

头部插入:将新节点作为链表的头节点,并更新head指针。

js中如何实现单链表

insertAtHead(value) {
  const newNode = new ListNode(value);
  if (!this.head) {
    this.head = newNode;
    this.tail = newNode;
  } else {
    newNode.next = this.head;
    this.head = newNode;
  }
  this.length++;
}

尾部插入:将新节点追加到链表末尾,并更新tail指针。

insertAtTail(value) {
  const newNode = new ListNode(value);
  if (!this.head) {
    this.head = newNode;
    this.tail = newNode;
  } else {
    this.tail.next = newNode;
    this.tail = newNode;
  }
  this.length++;
}

实现删除操作

删除头部节点:移除头节点并更新head指针。

deleteAtHead() {
  if (!this.head) return null;
  const removedValue = this.head.value;
  this.head = this.head.next;
  this.length--;
  if (this.length === 0) this.tail = null;
  return removedValue;
}

删除尾部节点:遍历链表找到倒数第二个节点,更新其next指针。

js中如何实现单链表

deleteAtTail() {
  if (!this.head) return null;
  let current = this.head;
  let removedValue;
  if (this.length === 1) {
    removedValue = this.head.value;
    this.head = null;
    this.tail = null;
  } else {
    while (current.next !== this.tail) {
      current = current.next;
    }
    removedValue = this.tail.value;
    current.next = null;
    this.tail = current;
  }
  this.length--;
  return removedValue;
}

遍历链表

通过循环从头节点开始依次访问每个节点。

traverse() {
  let current = this.head;
  while (current) {
    console.log(current.value);
    current = current.next;
  }
}

查找节点

根据值或索引查找链表中的节点。

findByValue(value) {
  let current = this.head;
  while (current) {
    if (current.value === value) return current;
    current = current.next;
  }
  return null;
}

findByIndex(index) {
  if (index < 0 || index >= this.length) return null;
  let current = this.head;
  for (let i = 0; i < index; i++) {
    current = current.next;
  }
  return current;
}

链表反转

通过迭代或递归反转链表。

reverse() {
  let prev = null;
  let current = this.head;
  while (current) {
    const next = current.next;
    current.next = prev;
    prev = current;
    current = next;
  }
  this.tail = this.head;
  this.head = prev;
}

示例用法

const list = new LinkedList();
list.insertAtTail(1);
list.insertAtTail(2);
list.insertAtHead(0);
list.traverse(); // 输出: 0, 1, 2
list.reverse();
list.traverse(); // 输出: 2, 1, 0

通过上述方法,可以实现单链表的基本操作,包括插入、删除、遍历和反转等。

分享给朋友:

相关文章

vue底层如何实现

vue底层如何实现

Vue 底层的核心实现机制 Vue 的底层实现主要围绕响应式系统、虚拟 DOM 和组件化架构展开。以下是关键技术的详细解析: 响应式系统 依赖追踪与发布订阅模式 通过 Object.definePr…

前段vue如何实现

前段vue如何实现

Vue 前端实现方法 Vue 是一款流行的前端框架,用于构建用户界面。以下是一些常见的 Vue 实现方法: 组件化开发 Vue 的核心思想是组件化开发。每个组件可以独立封装逻辑、模板和样式,便于复用…

vue 如何实现返回

vue 如何实现返回

Vue 实现返回功能的方法 使用 router.go(-1) 在 Vue 中可以通过 Vue Router 的 go 方法实现返回上一页的功能。在需要触发返回的按钮或方法中调用 this.$route…

vue如何实现webssh

vue如何实现webssh

使用Vue实现WebSSH WebSSH的实现需要结合前端Vue和后端服务,通常通过WebSocket协议与服务器进行实时通信。以下是具体实现方法: 安装必要依赖 在Vue项目中安装xterm.js…

vue如何实现标签

vue如何实现标签

实现动态标签页 在Vue中可以通过<component :is="currentTab">动态切换组件,结合v-for渲染标签页头部。典型结构包含标签页容器和内容区域: <temp…

vue如何实现vmodel

vue如何实现vmodel

Vue 中实现 v-model 的方法 v-model 是 Vue 中用于实现表单元素和数据双向绑定的指令。其本质是语法糖,结合了 value 属性和 input 事件的封装。以下是实现 v-mode…