AcWing 829. 數組模擬隊列(模板)

題目鏈接:點擊這裏
在這裏插入圖片描述
在這裏插入圖片描述
所有操作保證合法。

#include<iostream>
#include<string>

using namespace std;

const int N = 100010;

int q[N], hh = 0, tt = -1;  // hh 表示隊頭,tt表示隊尾

int main()
{
    int m;
    cin>>m;
    while(m--)
    {
        string op;
        int x;
        cin>>op;
        if(op=="push")
        {
            cin>>x;
            q[++tt] = x;
        }
        else if(op=="pop")
        {
            hh++;
        }
        else if(op=="empty")
        {
            cout << (hh <= tt ? "NO" : "YES") << endl;
        }
        else if(op=="query")
        {
            cout<<q[hh]<<endl;
        }
    }
    
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章