当前位置:首页 > JavaScript

js实现单向链表

2026-04-07 11:43:59JavaScript

单向链表的基本概念

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

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

实现单向链表的步骤

定义节点类

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

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

初始化链表

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

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. 插入节点(指定位置)
在指定索引处插入节点,更新相邻节点的指针:

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 属性:

js实现单向链表

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
分享给朋友:

相关文章

js实现轮播图

js实现轮播图

基础轮播图实现 使用HTML、CSS和JavaScript实现一个简单的自动轮播图。HTML结构包含一个容器和多个图片项。 <div class="slider"> <div…

js实现选题

js实现选题

实现选题功能的JavaScript方法 基础实现方案 使用数组存储选项,通过随机索引选取: const options = ['选项A', '选项B', '选项C', '选项D']; const r…

节流js实现

节流js实现

节流(Throttle)的实现原理 节流是一种限制函数执行频率的技术,确保函数在一定时间间隔内最多执行一次。适用于高频触发事件(如滚动、输入、窗口调整等)的场景。 基础实现方式 使用时间戳判断是否执…

js实现视口

js实现视口

js实现视口检测的方法 使用JavaScript检测元素是否进入视口(viewport)可以通过Intersection Observer API或手动计算元素位置实现。以下是两种常见方法: Int…

js实现上传文件

js实现上传文件

文件上传的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API可以实现文件上传功能。 <input type="file"…

js实现定位

js实现定位

使用Geolocation API获取当前位置 在JavaScript中,可以通过浏览器内置的Geolocation API获取用户的地理位置信息。该API需要用户授权才能访问位置数据。 if (n…