当前位置:首页 > JavaScript

js队列实现

2026-04-05 23:13:56JavaScript

队列的基本概念

队列是一种先进先出(FIFO)的数据结构,支持从队尾插入元素(入队),从队头移除元素(出队)。JavaScript中可通过数组或链表实现队列。

基于数组的实现

利用数组的 pushshift 方法模拟队列操作:

class ArrayQueue {
  constructor() {
    this.items = [];
  }

  // 入队
  enqueue(element) {
    this.items.push(element);
  }

  // 出队
  dequeue() {
    if (this.isEmpty()) return "Queue is empty";
    return this.items.shift();
  }

  // 查看队头
  peek() {
    if (this.isEmpty()) return null;
    return this.items[0];
  }

  // 检查是否为空
  isEmpty() {
    return this.items.length === 0;
  }

  // 返回队列大小
  size() {
    return this.items.length;
  }

  // 清空队列
  clear() {
    this.items = [];
  }
}

注意shift() 操作的时间复杂度为 O(n),频繁出队时性能较差。

基于链表的实现

通过链表节点维护队列结构,避免数组移动的开销:

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

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

  // 入队
  enqueue(value) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      this.tail.next = newNode;
      this.tail = newNode;
    }
    this.length++;
  }

  // 出队
  dequeue() {
    if (!this.head) return null;
    const removedValue = this.head.value;
    this.head = this.head.next;
    this.length--;
    if (this.length === 0) this.tail = null;
    return removedValue;
  }

  // 查看队头
  peek() {
    return this.head?.value ?? null;
  }

  // 检查是否为空
  isEmpty() {
    return this.length === 0;
  }

  // 返回队列大小
  size() {
    return this.length;
  }
}

优势:链表实现的入队和出队操作均为 O(1) 时间复杂度。

循环队列(优化版)

解决数组实现中空间浪费的问题:

class CircularQueue {
  constructor(capacity) {
    this.items = new Array(capacity);
    this.capacity = capacity;
    this.head = 0;
    this.tail = -1;
    this.size = 0;
  }

  // 入队
  enqueue(element) {
    if (this.isFull()) return false;
    this.tail = (this.tail + 1) % this.capacity;
    this.items[this.tail] = element;
    this.size++;
    return true;
  }

  // 出队
  dequeue() {
    if (this.isEmpty()) return null;
    const element = this.items[this.head];
    this.head = (this.head + 1) % this.capacity;
    this.size--;
    return element;
  }

  // 检查是否为空
  isEmpty() {
    return this.size === 0;
  }

  // 检查是否已满
  isFull() {
    return this.size === this.capacity;
  }
}

适用场景:需要固定大小队列时(如消息缓冲池)。

实际应用示例

任务调度:使用队列管理异步任务顺序执行:

js队列实现

const taskQueue = new ArrayQueue();
taskQueue.enqueue(() => console.log("Task 1"));
taskQueue.enqueue(() => console.log("Task 2"));

while (!taskQueue.isEmpty()) {
  const task = taskQueue.dequeue();
  task(); // 依次输出 "Task 1", "Task 2"
}

标签: 队列js
分享给朋友:

相关文章

js实现继承

js实现继承

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

php 实现队列

php 实现队列

PHP 实现队列的方法 PHP 可以通过多种方式实现队列功能,以下是几种常见的实现方法: 使用数组实现队列 PHP 数组可以通过 array_push 和 array_shift 函数模拟队列的先进…

js实现变形

js实现变形

使用 CSS 和 JavaScript 实现元素变形 在 JavaScript 中实现变形效果通常结合 CSS 的 transform 属性,通过动态修改该属性实现元素的旋转、缩放、倾斜或平移。 基…

js树实现

js树实现

树的基本概念 树是一种非线性的数据结构,由节点和边组成。每个节点包含一个值和指向子节点的引用。树的顶部节点称为根节点,没有子节点的节点称为叶节点。 树的实现方式 在JavaScript中,树可以通过…

js 实现递归

js 实现递归

递归的基本概念 递归是指函数直接或间接调用自身的过程。在JavaScript中,递归通常用于解决可以分解为相似子问题的问题,如阶乘、斐波那契数列、树形结构遍历等。 递归的实现要点 基线条件(Base…

js实现排序

js实现排序

数组排序方法 JavaScript提供了内置的sort()方法用于数组排序。默认情况下,sort()将元素转换为字符串并按照Unicode码点排序。对于数字排序,需传入比较函数。 const num…