关于队列queue的简单基础语句练习

Easy - STL-queue

给定一个初始为空的队列,执行下面几种操作n次:

1 x :表示在队列的末尾添加数字x x(1 ≤ x ≤ 100)。

2 :输出队首的数字。

3 :删除队首的数字(保证此时数组不为空)。

4 :输出队列中含有的数字个数。

Input

第一行,一个整数n(1 ≤ n ≤ 20),表示操作的次数。

第2到n+1行,表示每一种操作。

Output

每次操作如果需要输出则输出一行数字。

Sample 1

InputcopyOutputcopy
7
1 7
2
1 6
2
3
4
2
7
7
1
6

#include <iostream>
#include <queue>
using namespace std;
int main()
{
    int n;
    cin >>n;
    queue<int> q;
    while (n--)
    {
        int n;
        cin >>n;
        if (n==1)
        {
            int a;
            cin >>a;
            q.push(a);
        }
        else if (n==2)
        {
            if (!q.empty()) cout << q.front() << endl;
        }
        else if (n==3)
        {
            if (!q.empty()) q.pop();
        }
        else if (n==4)
        {
            cout << q.size() << endl;
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值