0
点赞
收藏
分享

微信扫一扫

关于如何用数组模拟队列 c++

吴陆奇 2022-03-12 阅读 97
#include <bits/stdc++.h>
using namespace std;
int arr[100005];
int main() {
    int n;
    cin>>n;
    int cnt = 0;
    int top = cnt;
    while (n--){
        string s;
        cin>>s;
        if (s == "push"){
            int k;//插入的数
            cin>>k;
            arr[cnt] = k;
            top = cnt;
            cnt++;
        }
        else if(s == "pop"){
            arr[top] = 0;
            top--;
            cnt--;
        }
        else if(s == "empty"){
            if (!arr[top]){
                cout<<"YES"<<endl;
            }
            else{
                cout<<"NO"<<endl;
            }
        }
        else if(s == "query"){
            cout<<arr[top]<<endl;
        }
    }
    return 0;
}
举报

相关推荐

0 条评论