很简单的利用STL的stack和queue即可解决
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
/*测试用例:
4
4 FIFO
IN 1
IN 2
OUT
OUT
4 FILO
IN 1
IN 2
OUT
OUT
5 FIFO
IN 1
IN 2
OUT
OUT
OUT
5 FILO
IN 1
IN 2
OUT
IN 3
OUT
1
2
2
1
1
2
None
2
3
*/
int main()
{
int n;
cin >> n;
while (n--)
{
int num;
string l;
queue<int> q;
stack<int> s;
cin >> num >> l;
if (l == "FIFO")
{
while (num--)
{
string str;
cin >> str;
if (str == "IN")
{
int num;
cin >> num;
q.push(num);
}
else
{
if (q.empty())
cout << "None";
else
{
cout << q.front()<<endl;
q.pop();
}
}
}
}
else
{
while (num--)
{
string str;
cin >> str;
if (str == "IN")
{
int num;
cin >> num;
s.push(num);
}
else
{
if (s.empty())
cout << "None";
else
{
cout << s.top()<<endl;
s.pop();
}
}
}
}
}
return 0;
}