信息发布→ 登录 注册 退出

javascript链表如何实现_如何操作节点的数据

发布时间:2025-12-26

点击量:
JavaScript链表需手动实现,核心是用对象模拟节点并通过next指针串联;所有操作(访问、修改、插入、删除)必须从头节点遍历,无法随机访问。

JavaScript 中链表不是内置数据结构,需要手动实现。核心是用对象模拟节点,通过 next(和可选的 prev)指针串联。操作节点数据的关键在于:**访问、修改、插入、删除都必须从头节点出发,沿指针逐个遍历,不能像数组那样随机访问。**

节点与链表的基本结构

每个节点是一个包含数据和指向下一节点引用的对象:

class ListNode {
  constructor(data) {
    this.data = data;
    this.next = null; // 指向下一个节点
  }
}

链表本身通常只保存头节点(head),它是入口点:

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

空链表即 this.head === null;只要头节点存在,就能顺着 next 一路访问后续所有节点。

如何读取和修改节点的数据

必须从头开始遍历,找到目标位置或满足条件的节点后,才能读或写 node.data

  • 读取第 i 个节点的数据(索引从 0 开始):
    getNodeDataAt(index) {
          let current = this.head;
          let count = 0;
          while (current !== null && count < index) {
            current = current.next;
            count++;
          }
          return current ? current.data : undefined;
        }
  • 修改第 i 个节点的数据:
    setNodeDataAt(index, newData) {
          let current = this.head;
          let count = 0;
          while (current !== null && count < index) {
            current = current.next;
            count++;
          }
          if (current) current.data = newData;
        }
  • 按值查找并更新第一个匹配节点:
    updateFirstMatch(oldValue, newValue) {
          let current = this.head;
          while (current !== null) {
            if (current.data === oldValue) {
              current.data = newValue;
              return true;
            }
            current = current.next;
          }
          return false;
        }

如何插入和删除节点(影响数据布局)

插入/删除会改变节点之间的连接关系,进而影响后续遍历顺序和数据可见性:

  • 在头部插入新节点(最简单):
    prepend(data) {
          const newNode = new ListNode(data);
          newNode.next = this.head;
          this.head = newNode;
        }
  • 在尾部插入:
    append(data) {
          const newNode = new ListNode(data);
          if (!this.head) {
            this.head = newNode;
            return;
          }
          let current = this.head;
          while (current.next !== null) {
            current = current.next;
          }
          current.next = newNode;
        }
  • 删除值为 target 的第一个节点:
    deleteByValue(target) {
          if (!this.head) return;
          if (this.head.data === target) {
            this.head = this.head.next;
            return;
          }
          let current = this.head;
          while (current.next && current.next.data !== target) {
            current = current.next;
          }
          if (current.next) {
            current.next = current.next.next;
          }
        }

遍历链表并批量操作数据

实际开发中常需对所有节点数据做统一处理,比如打印、求和、过滤:

  • 基础遍历(推荐 while 循环,清晰且不易越界):
    traverse(callback) {
          let current = this.head;
          while (current !== null) {
            callback(current.data);
            current = current.next;
          }
        }
    
    // 使用示例:打印所有数据
    list.traverse(data => console.log(data));
  • 转成数组便于调试或后续处理:
    toArray() {
          const arr = [];
          let current = this.head;
          while (current !== null) {
            arr.push(current.data);
            current = current.next;
          }
          return arr;
        }
  • 查找所有匹配的数据(返回数组):
    findAllByCondition(predicate) {
          const result = [];
          let current = this.head;
          while (current !== null) {
            if (predicate(current.data)) {
              result.push(current.data);
            }
            current = current.next;
          }
          return result;
        }
    
    // 使用示例:找出所有大于 10 的数
    const bigNumbers = list.findAllByCondition(x => x > 10);

链表的操作本质是“指针重连”,数据存于节点内,增删改查都依赖对 next 引用的正确操作。熟练掌握遍历模式,就能稳定地操作任意节点的数据。不复杂但容易忽略边界——比如空链表、单节点、删头节点等场景,写逻辑时多检查 current 是否为 null 就能避免报错。

标签:# javascript  # java  # node  # app  # red  
在线客服
服务热线

服务热线

4008888355

微信咨询
二维码
返回顶部
×二维码

截屏,微信识别二维码

打开微信

微信号已复制,请打开微信添加咨询详情!