当前位置:首页 > JavaScript

js实现单向链表

2026-02-02 18:01:03JavaScript

单向链表的基本概念

单向链表是一种线性数据结构,由多个节点组成,每个节点包含数据和指向下一个节点的指针。与数组不同,链表中的元素在内存中不是连续存储的,而是通过指针链接起来。

实现单向链表的步骤

定义节点类

每个节点需要存储数据和指向下一个节点的引用。可以使用类或构造函数实现。

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

定义链表类

链表类需要管理节点的添加、删除和遍历等操作。

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

添加节点到链表末尾

add(data) {
  const node = new Node(data);
  if (!this.head) {
    this.head = node;
  } else {
    let current = this.head;
    while (current.next) {
      current = current.next;
    }
    current.next = node;
  }
  this.size++;
}

在指定位置插入节点

insertAt(data, index) {
  if (index < 0 || index > this.size) {
    return false;
  }
  const node = new Node(data);
  if (index === 0) {
    node.next = this.head;
    this.head = node;
  } else {
    let current = this.head;
    let prev;
    let i = 0;
    while (i < index) {
      prev = current;
      current = current.next;
      i++;
    }
    node.next = current;
    prev.next = node;
  }
  this.size++;
}

从链表中移除指定位置的节点

removeFrom(index) {
  if (index < 0 || index >= this.size) {
    return null;
  }
  let current = this.head;
  if (index === 0) {
    this.head = current.next;
  } else {
    let prev;
    let i = 0;
    while (i < index) {
      prev = current;
      current = current.next;
      i++;
    }
    prev.next = current.next;
  }
  this.size--;
  return current.data;
}

打印链表中的所有元素

print() {
  let current = this.head;
  let result = '';
  while (current) {
    result += current.data + ' -> ';
    current = current.next;
  }
  result += 'null';
  console.log(result);
}

完整代码示例

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

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

  add(data) {
    const node = new Node(data);
    if (!this.head) {
      this.head = node;
    } else {
      let current = this.head;
      while (current.next) {
        current = current.next;
      }
      current.next = node;
    }
    this.size++;
  }

  insertAt(data, index) {
    if (index < 0 || index > this.size) {
      return false;
    }
    const node = new Node(data);
    if (index === 0) {
      node.next = this.head;
      this.head = node;
    } else {
      let current = this.head;
      let prev;
      let i = 0;
      while (i < index) {
        prev = current;
        current = current.next;
        i++;
      }
      node.next = current;
      prev.next = node;
    }
    this.size++;
  }

  removeFrom(index) {
    if (index < 0 || index >= this.size) {
      return null;
    }
    let current = this.head;
    if (index === 0) {
      this.head = current.next;
    } else {
      let prev;
      let i = 0;
      while (i < index) {
        prev = current;
        current = current.next;
        i++;
      }
      prev.next = current.next;
    }
    this.size--;
    return current.data;
  }

  print() {
    let current = this.head;
    let result = '';
    while (current) {
      result += current.data + ' -> ';
      current = current.next;
    }
    result += 'null';
    console.log(result);
  }
}

// 使用示例
const list = new LinkedList();
list.add(1);
list.add(2);
list.add(3);
list.print(); // 输出: 1 -> 2 -> 3 -> null
list.insertAt(4, 1);
list.print(); // 输出: 1 -> 4 -> 2 -> 3 -> null
list.removeFrom(2);
list.print(); // 输出: 1 -> 4 -> 3 -> null

单向链表的常见操作

查找节点

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

判断链表是否为空

isEmpty() {
  return this.size === 0;
}

获取链表长度

getSize() {
  return this.size;
}

反转链表

js实现单向链表

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

注意事项

  • 链表的操作需要注意边界条件,例如空链表或只有一个节点的链表。
  • 插入和删除操作需要正确处理指针的指向,避免内存泄漏或丢失节点。
  • 链表的遍历通常使用循环实现,注意循环的终止条件。

标签: 链表js
分享给朋友:

相关文章

js实现图片放大缩小

js实现图片放大缩小

实现图片放大缩小的基础方法 使用CSS的transform: scale()属性可以快速实现图片的放大缩小效果。通过JavaScript动态修改该属性值: const img = document.…

jquery.js

jquery.js

jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画设计和 Ajax 交互等操作。以下是关于 jQuery.js 的核心信息和使用方法: 获取 jQ…

js实现祖玛

js实现祖玛

实现祖玛游戏的核心思路 祖玛游戏的核心玩法是发射彩色珠子,形成三个或以上相同颜色的珠子即可消除。以下是使用JavaScript实现的基本框架。 游戏初始化 创建画布并初始化游戏状态: const…

js实现vue路由

js实现vue路由

Vue 路由的基本实现 在 Vue.js 中实现路由功能通常使用 Vue Router 库。Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。 安装 Vue R…

js 实现滚动

js 实现滚动

实现滚动的方法 使用 window.scrollTo() window.scrollTo() 方法可以滚动到文档中的特定位置。可以指定 x 和 y 坐标,或者使用平滑滚动的选项。 // 滚动到指定位…

js计算器的实现

js计算器的实现

实现基础结构 HTML部分需要创建计算器的基本框架,包含显示区域和按钮。使用<div>容器包裹整个计算器,内部设置一个显示屏和多个按钮。 <div class="calculato…