#include <iostream>
#include <stack>
#include <queue>
using namespace std;
int main(int argc, char* argv[])
{
int N,num,num1;
char str[12];
cin>>N;
while(N--)
{
cin>>num>>str;
//scanf("%d%s",&num,str);
if('F'==str[2])
{
queue<int> b; //这个地方自己出现了问题
while(num--)
{
cin>>str;
if('I'==str[0])
{
cin>>num1;
b.push(num1);
}
else
{
if(!b.empty())
{
cout<<b.front()<<endl;
//printf("%d\n",b.front());
b.pop();
}
else
cout<<"None"<<endl;
}
}
}
else
{
stack<int> a;
while(num--)
{
cin>>str;
//scanf("%d %s",&num1,str);
if('I'==str[0])
{
cin>>num1;
a.push(num1);
}
else
{
if(!a.empty())
{
cout<<a.top()<<endl;
a.pop();
}
else
cout<<"None"<<endl;
}
}
}
}
return 0;
}
找这个题目就是想要练习C++库中堆栈的使用的。
题目内容不是很难,自己犯了一个小错误,另有一个点现在还没有想通,看看提问会有神马收获没~
while(num--)
{
cin>>str;
if('I'==str[0])
{
cin>>num1;
b.push(num1);
}
这段代码开始自己写成了下面的形式,结果多输入时是无限循环。自己对于cin的机制和应用还不了解,上网也没有找到相关的资料。有高手可以帮忙解答下不~O(∩_∩)O~
while(num--){ cin>>str>>num1; if('I'==str[0]) { b.push(num1); }