模拟队列

    科技2026-01-11  11

    模拟队列

    变量解释:

    t指向尾指针,h指向头指针

    初始化的时候h要比t大1,因为第一个插入(只有一个元素)的时候h和t应该指向同一个位置

    代码:

    #include<iostream> using namespace std; const int N = 100010; //初始化的时候尾要比头少1,当h = t 的时候只有一个元素,当h > t 的时候为空 int h=1,t,que[N]; int main() { int M; cin>>M; while(M --) { string op; cin>>op; int k,x; if( op == "push") { cin>>x; que[++t] = x; }else if(op == "pop") { h++; }else if(op == "empty") { cout<<(t >= h?"NO":"YES")<<endl; }else { cout<<que[h]<<endl; } } return 0; }
    Processed: 0.018, SQL: 9