队列(模拟队列)

题目:

队列(模拟队列) 

代码:

#include <iostream>
using namespace std;
const int N = 100010;
int q[N],hh,tt=-1;  //hh表示队头,tt表示队尾

int main()
{
    int n;
    cin >> n;
    while(n--)
    {
        string s;
        cin >> s;
        
        //在队尾插入元素
        if(s == "push")
        {
            int x;
            cin >> x;
            q[++tt] = x;
        }

        //在队头弹出元素
        if(s == "pop")
        {
            hh ++;
        }
        
        //取出队头元素
        if(s == "query")
        {
            cout << q[hh] << endl;
        }
       
        //判断队列是否为空
        if(s == "empty")
        {
            cout << ((hh <= tt) ? "NO" : "YES") << endl;
        }
    }
    return 0;
}

上一篇:5W1H聊开源之Why——为什么要参与开源?


下一篇:单调队列模板&滑动窗口