当前位置:首页 > JavaScript

js实现队列

2026-02-02 05:47:57JavaScript

队列的基本概念

队列是一种遵循先进先出(FIFO)原则的线性数据结构。元素从队尾入队,从队头出队,操作包括enqueue(入队)、dequeue(出队)、peek(查看队头元素)和isEmpty(判断是否为空)。

使用数组实现队列

数组实现简单直观,但出队操作(shift)的时间复杂度为O(n),因为需要移动剩余元素。

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 "Queue is empty";
    return this.items[0];
  }

  isEmpty() {
    return this.items.length === 0;
  }

  size() {
    return this.items.length;
  }
}

使用链表实现队列

链表实现避免了数组出队的性能问题,所有操作均为O(1)时间复杂度。

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.isEmpty()) {
      this.front = newNode;
    } else {
      this.rear.next = newNode;
    }
    this.rear = newNode;
    this.size++;
  }

  dequeue() {
    if (this.isEmpty()) return "Queue is empty";
    const removedValue = this.front.value;
    this.front = this.front.next;
    if (!this.front) this.rear = null;
    this.size--;
    return removedValue;
  }

  peek() {
    if (this.isEmpty()) return "Queue is empty";
    return this.front.value;
  }

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

使用两个栈实现队列

通过维护输入栈和输出栈,模拟队列行为。摊还时间复杂度为O(1)。

class StackQueue {
  constructor() {
    this.inStack = [];
    this.outStack = [];
  }

  enqueue(value) {
    this.inStack.push(value);
  }

  dequeue() {
    if (this.outStack.length === 0) {
      while (this.inStack.length > 0) {
        this.outStack.push(this.inStack.pop());
      }
    }
    return this.outStack.pop() || "Queue is empty";
  }

  peek() {
    if (this.outStack.length === 0) {
      while (this.inStack.length > 0) {
        this.outStack.push(this.inStack.pop());
      }
    }
    return this.outStack[this.outStack.length - 1] || "Queue is empty";
  }

  isEmpty() {
    return this.inStack.length === 0 && this.outStack.length === 0;
  }
}

循环队列实现

固定大小的循环队列能有效利用空间,通过模运算实现环形结构。

js实现队列

class CircularQueue {
  constructor(k) {
    this.size = k;
    this.queue = new Array(k);
    this.head = -1;
    this.tail = -1;
  }

  enqueue(value) {
    if (this.isFull()) return false;
    if (this.isEmpty()) this.head = 0;
    this.tail = (this.tail + 1) % this.size;
    this.queue[this.tail] = value;
    return true;
  }

  dequeue() {
    if (this.isEmpty()) return false;
    if (this.head === this.tail) {
      this.head = -1;
      this.tail = -1;
    } else {
      this.head = (this.head + 1) % this.size;
    }
    return true;
  }

  Front() {
    return this.isEmpty() ? -1 : this.queue[this.head];
  }

  Rear() {
    return this.isEmpty() ? -1 : this.queue[this.tail];
  }

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

  isFull() {
    return (this.tail + 1) % this.size === this.head;
  }
}

性能对比

  • 数组队列:简单但出队性能差,适合数据量小的场景。
  • 链表队列:所有操作O(1),适合频繁出入队的场景。
  • 栈实现队列:摊还O(1),但需要维护两个栈。
  • 循环队列:空间利用率高,适合固定容量需求。

选择实现方式时应根据具体场景考虑性能需求和空间限制。

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

相关文章

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 或直接使用 window.location 实现页面跳转,适用于普通跳转或带参数的 URL。 // 方…

js实现倒计时

js实现倒计时

使用 setInterval 实现倒计时 通过 setInterval 定时器每秒更新剩余时间,适用于简单倒计时场景。 function countdown(seconds, callback) {…

vue实现队列

vue实现队列

Vue 实现队列功能 在 Vue 中实现队列功能,可以利用数组的 push 和 shift 方法模拟队列的先进先出(FIFO)特性。以下是一个简单的实现示例: <template>…

php队列的实现

php队列的实现

PHP队列的实现方式 PHP中实现队列功能可以通过多种方式,根据需求选择适合的方案: 使用数组实现基础队列 PHP数组本身支持队列操作,利用array_push和array_shift函数: $q…

js实现变形

js实现变形

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

js类实现

js类实现

使用类实现 JavaScript 功能 在 JavaScript 中,类(Class)是一种语法糖,基于原型继承机制。通过 class 关键字可以更直观地定义对象模板。 基本类定义 class…