当前位置:首页 > 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;
  }
}

添加节点到链表末尾

js实现单向链表

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

打印链表中的所有元素

js实现单向链表

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

反转链表

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实现vue

js实现vue

Vue.js 简介 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。其核心库专注于视图层,易于与其他库或现有项目整合。 实现 Vue.js 的基本步骤 安装 Vue.j…

js实现计算器

js实现计算器

实现基础计算器功能 使用JavaScript创建一个基础计算器需要处理数字输入、运算符逻辑和结果显示。以下是一个简单的实现示例: let currentInput = '0'; let previo…

js 实现继承

js 实现继承

原型链继承 通过让子类的原型对象指向父类的实例来实现继承。子类实例可以访问父类原型上的属性和方法。 function Parent() { this.name = 'parent'; } Par…

js 实现vue

js 实现vue

Vue.js 的基本实现 在 JavaScript 中实现 Vue.js 的核心功能,可以通过数据绑定、响应式系统和虚拟 DOM 来实现。以下是实现 Vue.js 核心功能的简化版本。 数据响应式系…

js怎么实现异步

js怎么实现异步

异步实现方法 JavaScript中实现异步操作主要通过以下几种方式: Promise Promise是ES6引入的异步解决方案,用于处理异步操作的成功或失败状态。 const promise =…

js实现搜索

js实现搜索

实现搜索功能的方法 在JavaScript中实现搜索功能可以通过多种方式完成,以下是几种常见的实现方法。 使用数组的filter方法 通过数组的filter方法可以筛选出符合条件的数据项。假设有一个…