当前位置:首页 > 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
分享给朋友:

相关文章

js实现选项卡

js实现选项卡

实现选项卡的基本思路 选项卡通常由一组标签和对应的内容面板组成。点击标签时,显示对应的内容面板,隐藏其他面板。实现这一效果需要结合HTML结构、CSS样式和JavaScript交互逻辑。 HTML结…

js实现列表

js实现列表

使用 JavaScript 实现列表 JavaScript 提供了多种方式来实现列表功能,包括数组操作、DOM 元素动态生成等。以下是几种常见的实现方法: 使用数组存储列表数据 数组是 JavaSc…

js实现文字滚动

js实现文字滚动

实现文字滚动的几种方法 使用CSS动画实现滚动 通过CSS的@keyframes和transform属性可以实现平滑的文字滚动效果。 <style> .scroll-text { w…

js实现上传文件

js实现上传文件

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

js实现下拉刷新

js实现下拉刷新

监听触摸事件 通过监听 touchstart、touchmove 和 touchend 事件来检测用户下拉手势。记录触摸起始位置和移动距离。 let startY = 0; let currentY…

uniapp队列下载

uniapp队列下载

uniapp 实现队列下载的方法 在 uniapp 中实现队列下载通常需要管理多个下载任务,确保它们按顺序执行。可以通过以下方法实现: 使用 Promise 和递归实现队列下载 定义一个下载队列数…