当前位置:首页 > JavaScript

js实现单向链表

2026-04-07 11:43:59JavaScript

单向链表的基本概念

单向链表是一种线性数据结构,由多个节点组成,每个节点包含两部分:

  • 数据域(value):存储实际数据。
  • 指针域(next):指向下一个节点的引用(最后一个节点指向 null)。

实现单向链表的步骤

定义节点类

使用 class 或构造函数定义链表节点,包含 valuenext 属性:

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

初始化链表

创建链表类,初始化头节点和链表长度:

js实现单向链表

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

实现常用方法

1. 添加节点(尾部)
遍历链表至末尾,将新节点连接到最后一个节点的 next

append(value) {
  const newNode = new ListNode(value);
  if (!this.head) {
    this.head = newNode;
  } else {
    let current = this.head;
    while (current.next) {
      current = current.next;
    }
    current.next = newNode;
  }
  this.length++;
}

2. 插入节点(指定位置)
在指定索引处插入节点,更新相邻节点的指针:

js实现单向链表

insert(index, value) {
  if (index < 0 || index > this.length) return false;
  const newNode = new ListNode(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;
    }
    newNode.next = current;
    prev.next = newNode;
  }
  this.length++;
  return true;
}

3. 删除节点
根据索引删除节点,调整前后节点的指针:

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

4. 查找节点
根据值返回节点索引,未找到返回 -1

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

5. 获取链表长度
直接返回 length 属性:

size() {
  return this.length;
}

完整代码示例

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

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

  append(value) {
    const newNode = new ListNode(value);
    if (!this.head) {
      this.head = newNode;
    } else {
      let current = this.head;
      while (current.next) {
        current = current.next;
      }
      current.next = newNode;
    }
    this.length++;
  }

  insert(index, value) {
    if (index < 0 || index > this.length) return false;
    const newNode = new ListNode(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;
      }
      newNode.next = current;
      prev.next = newNode;
    }
    this.length++;
    return true;
  }

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

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

  size() {
    return this.length;
  }
}

使用示例

const list = new LinkedList();
list.append(10);
list.append(20);
list.insert(1, 15);
console.log(list.indexOf(15)); // 输出: 1
list.removeAt(1);
console.log(list.size());      // 输出: 2

通过以上实现,可以完成单向链表的基本操作,包括增删查改和长度获取。

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

相关文章

css3结合js制作

css3结合js制作

CSS3 结合 JavaScript 制作动态效果 CSS3 和 JavaScript 结合可以实现丰富的动态效果,包括动画、交互和响应式设计。以下是几种常见的实现方式: CSS3 动画与 Java…

js 实现继承

js 实现继承

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

jquery.js

jquery.js

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

js实现自举

js实现自举

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

js实现路由

js实现路由

js实现路由的方法 在JavaScript中实现路由功能可以通过多种方式完成,以下是几种常见的方法: 使用原生JavaScript实现路由 通过监听window.onhashchange事件来实现基…

js实现删除

js实现删除

使用 splice 方法删除数组元素 splice 方法可以删除数组中的元素,并返回被删除的元素。它接受两个参数:起始索引和要删除的元素数量。 const array = [1, 2, 3, 4…