HDU-1702-ACboy needs your help again!(Stack)

队列和栈的判空都可以用empty

#include <bits/stdc++.h>
using namespace std;

string oper,stru;
int T,M,num;

int main()
{
    cin>>T;
    while (T--) {
        cin>>M>>stru;
        if (stru=="FIFO") {
            queue<int>q;
            for (int i=0;i<M;i++) {
                cin>>oper;
                if (oper=="IN") {
                    cin>>num;
                    q.push(num);
                }
                else {
                    if (!q.empty()) {
                        cout<<q.front()<<endl;
                        q.pop();
                    }
                    else {
                        cout<<"None"<<endl;
                    }
                }
            }
        }
        else {
            stack<int>s;
            for (int i=0;i<M;i++) {
                cin>>oper;
                if (oper=="IN") {
                    cin>>num;
                    s.push(num);
                }
                else {
                    if (!s.empty()) {
                        cout<<s.top()<<endl;
                        s.pop();
                    }
                    else {
                        cout<<"None"<<endl;
                    }
                }
            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/xyqxyq/p/12328874.html