数据结构---队列

前言

队列是一种先进先出(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;
相关推荐
Fanxt_Ja2 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
今后1232 天前
【数据结构】二叉树的概念
数据结构·二叉树
散1123 天前
01数据结构-01背包问题
数据结构
消失的旧时光-19433 天前
Kotlinx.serialization 使用讲解
android·数据结构·android jetpack
Gu_shiwww3 天前
数据结构8——双向链表
c语言·数据结构·python·链表·小白初步
苏小瀚3 天前
[数据结构] 排序
数据结构
睡不醒的kun3 天前
leetcode算法刷题的第三十四天
数据结构·c++·算法·leetcode·职场和发展·贪心算法·动态规划
吃着火锅x唱着歌3 天前
LeetCode 978.最长湍流子数组
数据结构·算法·leetcode
Whisper_long3 天前
【数据结构】深入理解堆:概念、应用与实现
数据结构
IAtlantiscsdn3 天前
Redis7底层数据结构解析
前端·数据结构·bootstrap