题解 | #【模板】队列#
【模板】队列
https://www.nowcoder.com/practice/afe812c80ad946f4b292a26dd13ba549?tpId=308&tqId=2110348&ru=/exam/oj&qru=/ta/algorithm-start/question-ranking&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D308
#include <iostream>
using namespace std;
#include<algorithm>
#include <string>
class stack{
private:
int s[100000];
int top=-1;
public:
void push(int x)
{
top++;
s[top]=x;
}
void pop()
{
// if(top>=0)
// {
// if(top==0)
// {
// cout<<s[0]<<endl;
// top=-1;
// }
// else
// {
// cout<<s[0]<<endl;
// reverse(s, s+top); //反转
// top--;
// reverse(s, s+top); //反转
// }
// }
if(top>=0)
{
cout<<s[0]<<endl;
for(int i=0;i<top;i++)
{
s[i]=s[i+1];
}
top--;
}
else cout<<"error"<<endl;
}
void front()
{
if(top>=0)
{
cout<<s[0]<<endl;
}
else cout<<"error"<<endl;
}
};
int main() {
// int a, b;
// while (cin >> a >> b) { // 注意 while 处理多个 case
// cout << a + b << endl;
// }
stack s;
int n=0;
cin >>n;
for(int i=0;i<n;i++)
{
string op;
cin>>op;
if(op=="push")
{
int a=0;
cin>>a;
s.push(a);
}
if(op=="pop")
{
s.pop();
}
if(op=="front")
{
s.front();
}
}
return 0;
}
// 64 位输出请用 printf("%lld")
腾讯成长空间 5958人发布