- 题目大意
给出了先进先出和先进后出的两种结构,分别对应队列和栈,并且每种均给出In和Out两类操作,如果是In,push进后面的数,如果是Out,输出栈顶(队首)。
- 解题思路
对于给的命令判断,然后来决定是用队列还是栈。
- 代码
#include<iostream>
#include<stack>
#include<queue>
using namespace std;
int main()
{
int n,x,b;
char a[5];
char c[5];
cin >> n;
while (n--)
{
cin >> x >> a;
if (strcmp(a, "FIFO"))
{
stack<int>num;
while (x--)
{
cin >> c;
if (!strcmp(c, "IN"))
{
cin >> b;
num.push(b);
}
else
{
if (num.empty())
{
cout << "None" << endl;
}
else
{
cout << num.top() << endl;
num.pop();
}
}
}
}
else
{
queue<int>sum;
while (x--)
{
cin >> c;
if (!strcmp(c, "IN"))
{
cin >> b;
sum.push(b);
}
else
{
if (sum.empty())
{
cout << "None" << endl;
}
else
{
cout << sum.front() << endl;
sum.pop();
}
}
}
}
}
return 0;
}