B3616 【模板】队列

· · 题解

B3616 【模板】队列 题解

题意:给你一个队列,让你进行加入,查询第一个,查询大小,弹出四个操作,你要输出两个查询操作的答案。

Algorithm 1

不了解队列的同学戳这里

运用STL中的队列,具体细节会在代码内详解:

#include<iostream>
#include<string.h>
#include<string>
#include<unordered_map>
#include<algorithm>
#include<stdio.h>
#include<queue>
using namespace std;
int n, m, i, j, k;
queue<int> q;
int main()
{
    cin >> n;
    for (i = 1; i <= n; i++)
    {
        int op, x;
        cin >> op;
        if (op == 1) cin >> x, q.push(x);//加入
        else if (op == 2)
        {
            if (q.empty()) cout << "ERR_CANNOT_POP\n";//要判断是否为空,否则RE
            else q.pop();//如果不为空,弹出
        }
        else if (op == 3)
        {
            if (q.empty()) cout << "ERR_CANNOT_QUERY\n";//同上
            else cout << q.front() << endl;//输出第一个
        }
        else cout << q.size() << endl;//输出队列大小
    }
    return 0;//结束
}

Algorithm 2

除了STL,我们还可以手写一个队列!

思路:

代码就不给大家了,有兴趣的同学可以自己试着写一写。