当前位置:首页 > JavaScript

js 实现链表

2026-02-28 18:57:13JavaScript

实现链表的基本结构

链表由节点(Node)组成,每个节点包含数据(value)和指向下一个节点的指针(next)。以下是基础链表的实现:

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

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

添加节点到链表末尾

使用 append 方法在链表末尾添加新节点。若链表为空,新节点成为头节点;否则遍历到链表末尾插入。

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

在指定位置插入节点

通过 insertAt 方法在特定索引处插入节点。需处理索引越界及头节点插入的特殊情况。

js 实现链表

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

删除指定位置的节点

removeFrom 方法根据索引删除节点。需处理头节点删除和中间节点删除的逻辑。

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

查找节点值的位置

indexOf 方法返回给定值的首次出现索引,若未找到则返回 -1。

js 实现链表

indexOf(value) {
  let current = this.head;
  let index = 0;
  while (current) {
    if (current.value === value) return index;
    current = current.next;
    index++;
  }
  return -1;
}

判断链表是否为空

通过 isEmpty 方法检查链表是否为空(即头节点是否为 null)。

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

获取链表长度

getSize 方法直接返回链表的 size 属性。

getSize() {
  return this.size;
}

打印链表内容

print 方法将链表所有节点的值拼接为字符串输出。

print() {
  let current = this.head;
  let str = "";
  while (current) {
    str += current.value + " -> ";
    current = current.next;
  }
  str += "null";
  console.log(str);
}

完整代码示例

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

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

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

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

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

  indexOf(value) {
    let current = this.head;
    let index = 0;
    while (current) {
      if (current.value === value) return index;
      current = current.next;
      index++;
    }
    return -1;
  }

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

  getSize() {
    return this.size;
  }

  print() {
    let current = this.head;
    let str = "";
    while (current) {
      str += current.value + " -> ";
      current = current.next;
    }
    str += "null";
    console.log(str);
  }
}

// 使用示例
const list = new LinkedList();
list.append(10);
list.append(20);
list.insertAt(15, 1);
list.print(); // 输出: 10 -> 15 -> 20 -> null
console.log(list.removeFrom(1)); // 输出: 15
console.log(list.indexOf(20)); // 输出: 1

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

相关文章

js实现vue

js实现vue

Vue.js 的基本实现方法 在 JavaScript 中实现 Vue.js 的功能,可以通过直接引入 Vue 库或使用现代构建工具(如 Vite 或 Webpack)。以下是几种常见的实现方式:…

js实现轮播图

js实现轮播图

轮播图实现方法 基础HTML结构 <div class="slider"> <div class="slides"> <img src="image1.jpg…

js实现报表

js实现报表

使用JavaScript实现报表 在JavaScript中实现报表功能可以通过多种方式完成,常见的方法包括使用原生JavaScript、第三方库(如Chart.js、D3.js)或结合后端数据渲染。以…

js 实现链表

js 实现链表

链表的基本概念 链表是一种线性数据结构,由一系列节点组成,每个节点包含数据和指向下一个节点的指针。与数组不同,链表在内存中是非连续存储的,插入和删除操作效率较高。 链表的实现 在 JavaScrip…

js实现祖玛

js实现祖玛

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

js实现自举

js实现自举

实现 JavaScript 自举的方法 JavaScript 自举(Bootstrap)通常指通过 JavaScript 动态加载自身或依赖的脚本,实现运行时环境的初始化或扩展。以下是几种常见实现方式…