腾讯 4月26日笔试 第4题 两个栈模拟队列 C++
#include <iostream>
#include <stack>
using namespace std;
int main()
{
stack<int> a,b;
int n;
cin >> n;
string op;
int num;
while (n--) {
cin >> op;
if (op == "add") {
cin >> num;
b.push(num);
}else if (op == "poll") {
if (a.empty()) {
while (!b.empty()) {
a.push(b.top());
b.pop();
}
}
a.pop();
}
else if (op == "peek") {
if (a.empty()) {
while (!b.empty()) {
a.push(b.top());
b.pop();
}
}
cout<<a.top()<<endl;
}
}
}