当前位置:首页 > 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 方法在特定索引处插入节点。需处理索引越界及头节点插入的特殊情况。

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。

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

相关文章

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 实现页面跳转,这是最常用的方法。 window.location.href = 'https://…

js实现轮播图

js实现轮播图

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

js实现打印

js实现打印

使用window.print()方法实现打印 在JavaScript中,可以通过调用window.print()方法直接触发浏览器的打印功能。这会打开系统的打印对话框,用户可以选择打印机和设置打印选项…

js实现图片上传

js实现图片上传

图片上传的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API可以实现图片上传功能。HTML部分需要创建一个文件选择输入框和一个用于…

js 实现继承

js 实现继承

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

js实现类

js实现类

JavaScript 实现类的几种方式 JavaScript 是一种基于原型的语言,但在 ES6 之后引入了 class 语法糖,使得实现类更加直观。以下是几种常见的实现方式: 使用构造函数 传统…