829. 模拟队列,手写队列

实现一个队列,队列初始为空,支持四种操作:

  1. push x -- 向队尾插入一个数 x;
  2. pop -- 从队头弹出一个数;
  3. empty -- 判断队列是否为空;
  4. query -- 查询队头元素。

现在要对队列进行 M 个操作,其中的每个操作 33 和操作 44 都要输出相应的结果。

输入格式

第一行包含整数 M,表示操作次数。

接下来 M 行,每行包含一个操作命令,操作命令为 push xpopemptyquery 中的一种。

输出格式

对于每个 emptyquery 操作都要输出一个查询结果,每个结果占一行。

其中,empty 操作的查询结果为 YESNOquery 操作的查询结果为一个整数,表示队头元素的值。

数据范围

1≤M≤100000,

1≤x≤109

所有操作保证合法。

输入样例:
10
push 6
empty
query
pop
empty
push 3
push 4
pop
query
push 6
输出样例:
NO
6
YES
4

代码 :

cpp 复制代码
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>
#include<unordered_map>
using namespace std;
typedef long long LL;
const int N = 1e5 + 5;
int n;
int q[N],hh,tt=-1;

int main(){
    cin>>n;
    char op[20];
    int x;
    while(n--){
        scanf("%s",op);
        if(!strcmp(op,"push")){
            scanf("%d",&x);
            q[++tt]=x;
        }
        else if(!strcmp(op,"empty")){
            if(hh<=tt)printf("NO\n");
            else printf("YES\n");
        }
        else if(!strcmp(op,"query")){
            printf("%d\n",q[hh]);
        }
        else if(!strcmp(op,"pop")){
            if(hh<=tt)
            hh++;
        }
    }
    return 0;
}
相关推荐
serve the people7 分钟前
神经网络中梯度计算求和公式求导问题
神经网络·算法·机器学习
闻缺陷则喜何志丹13 分钟前
【二分查找、滑动窗口】P10389 [蓝桥杯 2024 省 A] 成绩统计|普及+
c++·算法·蓝桥杯·二分查找·滑动窗口·洛谷·成绩
星空露珠1 小时前
迷你世界脚本显示板管理接口:DisPlayBoard
数据结构·游戏·lua
乔冠宇1 小时前
蓝桥杯算法——铠甲合体
算法·职场和发展·蓝桥杯
商bol451 小时前
算阶,jdk和idea的安装
数据结构·c++·算法
迷迭所归处1 小时前
C语言 —— 愿文明如薪火般灿烂 - 函数递归
c语言·开发语言·算法
柠檬鲨_4 小时前
C语言100天练习题【记录本】
c语言·数据结构·算法
float_六七4 小时前
二叉树三种遍历方式——前序、中序、后序(C++)
开发语言·c++·算法
CYRUS_STUDIO9 小时前
常用加解密算法介绍
算法·安全·逆向
weixin_5358542211 小时前
快手,蓝禾,优博讯,三七互娱,顺丰,oppo,游卡,汤臣倍健,康冠科技,作业帮,高途教育25届春招内推
java·前端·python·算法·硬件工程