数据结构---队列

前言

队列是一种先进先出(FIFO)的数据结构,它有两个主要操作:入队(enqueue)和出队(dequeue)。队列中的元素只能通过队尾入队,只能通过队头出队。

队列的特点

  1. 只能在队尾添加元素,在队首删除元素。
  2. 先进先出的原则。
  3. 适用于需要按照时间顺序处理的场景。

队列的常用方法

  1. enqueue(item): 向队列尾部添加一个或多个新的项。
  2. dequeue(): 移除队列的第一个项,并返回被移除的元素。
  3. head(): 返回队列第一个元素,队列不做任何变动。
  4. tail(): 返回队列最后一个元素,队列不做任何变动。
  5. isEmpty(): 队列内无元素返回 true,否则返回 false。
  6. size(): 返回队列内元素个数。
  7. clear(): 清空队列。

代码实现

队列的本质是数组,所以队列的方法就是对数组的再次封装

javascript 复制代码
class QUEUE {
    constructor() {
        this.queue = [];
    }
 
    enqueue(val) {
        this.queue.push(val);
    }
 
    dequeue() {
        this.queue.shift();
    }
 
    head() {
        return this.queue[0];
    }
 
    tail() {
        return this.queue[this.queue.length-1];
    }
 
    isEmpty() {
        return this.queue.length > 0 ? true : false;
    }
 
    size() {
        return this.queue.length;
    }
 
    clear() {
        return this.queue = []
    }
}
 
export default QUEUE;
相关推荐
多米Domi0118 小时前
0x3f第33天复习 (16;45-18:00)
数据结构·python·算法·leetcode·链表
曹仙逸8 小时前
数据结构day04
数据结构
Lips6119 小时前
2026.1.16力扣刷题
数据结构·算法·leetcode
曹仙逸9 小时前
数据结构day05
数据结构
睡一觉就好了。9 小时前
树的基本结构
数据结构
kaikaile199510 小时前
A星算法避开障碍物寻找最优路径(MATLAB实现)
数据结构·算法·matlab
今天_也很困10 小时前
LeetCode 热题100-15.三数之和
数据结构·算法·leetcode
思成Codes12 小时前
ACM训练:接雨水3.0——动态接雨水
数据结构·算法
sin_hielo12 小时前
leetcode 2943
数据结构·算法·leetcode
Snow_day.13 小时前
有关平衡树
数据结构·算法·贪心算法·动态规划·图论