当前位置:首页 > JavaScript

js 队列实现

2026-04-04 01:34:25JavaScript

队列的基本概念

队列是一种遵循先进先出(FIFO)原则的线性数据结构。元素的插入操作在队列的尾部进行,删除操作在队列的头部进行。

js 队列实现

数组实现队列

使用数组实现队列时,可以通过维护头部和尾部指针来模拟队列的行为。

js 队列实现

class Queue {
  constructor() {
    this.items = [];
    this.frontIndex = 0;
    this.rearIndex = 0;
  }

  enqueue(element) {
    this.items[this.rearIndex] = element;
    this.rearIndex++;
  }

  dequeue() {
    if (this.isEmpty()) return undefined;
    const removedItem = this.items[this.frontIndex];
    delete this.items[this.frontIndex];
    this.frontIndex++;
    return removedItem;
  }

  peek() {
    if (this.isEmpty()) return undefined;
    return this.items[this.frontIndex];
  }

  isEmpty() {
    return this.frontIndex === this.rearIndex;
  }

  size() {
    return this.rearIndex - this.frontIndex;
  }

  clear() {
    this.items = [];
    this.frontIndex = 0;
    this.rearIndex = 0;
  }
}

链表实现队列

链表实现队列可以避免数组实现的性能问题,尤其是在频繁的插入和删除操作中。

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

class LinkedListQueue {
  constructor() {
    this.front = null;
    this.rear = null;
    this.size = 0;
  }

  enqueue(value) {
    const newNode = new Node(value);
    if (this.rear === null) {
      this.front = newNode;
      this.rear = newNode;
    } else {
      this.rear.next = newNode;
      this.rear = newNode;
    }
    this.size++;
  }

  dequeue() {
    if (this.isEmpty()) return undefined;
    const removedNode = this.front;
    this.front = this.front.next;
    if (this.front === null) this.rear = null;
    this.size--;
    return removedNode.value;
  }

  peek() {
    if (this.isEmpty()) return undefined;
    return this.front.value;
  }

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

  getSize() {
    return this.size;
  }

  clear() {
    this.front = null;
    this.rear = null;
    this.size = 0;
  }
}

循环队列实现

循环队列是一种优化后的队列实现方式,可以有效利用数组空间。

class CircularQueue {
  constructor(capacity) {
    this.items = new Array(capacity);
    this.capacity = capacity;
    this.front = -1;
    this.rear = -1;
  }

  enqueue(element) {
    if (this.isFull()) return false;
    if (this.isEmpty()) this.front = 0;
    this.rear = (this.rear + 1) % this.capacity;
    this.items[this.rear] = element;
    return true;
  }

  dequeue() {
    if (this.isEmpty()) return undefined;
    const removedItem = this.items[this.front];
    if (this.front === this.rear) {
      this.front = -1;
      this.rear = -1;
    } else {
      this.front = (this.front + 1) % this.capacity;
    }
    return removedItem;
  }

  peek() {
    if (this.isEmpty()) return undefined;
    return this.items[this.front];
  }

  isEmpty() {
    return this.front === -1;
  }

  isFull() {
    return (this.rear + 1) % this.capacity === this.front;
  }

  size() {
    if (this.isEmpty()) return 0;
    return (this.rear - this.front + this.capacity) % this.capacity + 1;
  }
}

队列的应用场景

队列常用于需要按顺序处理数据的场景,例如任务调度、广度优先搜索(BFS)、消息队列等。选择合适的实现方式可以根据具体需求优化性能。

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

相关文章

vue实现js休眠

vue实现js休眠

Vue 中实现 JavaScript 休眠 在 Vue 中实现 JavaScript 休眠通常需要使用异步方式,以避免阻塞主线程。以下是几种常见方法: 使用 setTimeout 和 Promise…

js实现文件下载

js实现文件下载

使用 a 标签下载文件 通过动态创建 a 标签并设置 download 属性实现文件下载。适用于已知文件 URL 或 Blob 数据的情况。 function downloadFile(url, f…

php实现队列

php实现队列

PHP实现队列的方法 队列是一种先进先出(FIFO)的数据结构,PHP可以通过多种方式实现队列功能。 使用数组模拟队列 PHP数组可以模拟队列的基本操作,通过array_push和array_shi…

js实现延迟

js实现延迟

实现延迟的方法 在JavaScript中,实现延迟操作有多种方式,以下是几种常见的方法: 使用setTimeout函数 setTimeout是JavaScript中最常用的延迟执行方法。它接受一个回…

js实现投球

js实现投球

实现投球动画的基本思路 使用JavaScript和CSS动画结合的方式模拟投球效果。核心是通过改变元素的位置、旋转和缩放属性,配合定时器或CSS过渡实现平滑动画。 创建基础HTML结构 <…

js实现交换

js实现交换

交换变量的方法 在JavaScript中,交换两个变量的值有多种方法。以下是常见的几种实现方式: 使用临时变量 通过引入一个临时变量来存储其中一个变量的值,实现交换: let a = 1; le…