题解 | #【模板】队列#
【模板】队列
https://www.nowcoder.com/practice/afe812c80ad946f4b292a26dd13ba549
#include <iostream>
#include <string>
using namespace std;
class Queue
{
private:
int Container [100001];
int beginer=0;
int ender=0;
public:
void push(int x);
int pop();
int front ();
};
void Queue:: push (int x)
{
Container[ender]=x;
ender++;
}
int Queue:: pop()
{
int b=0;
if (beginer>=ender)
cout<<"error"<<endl;
else{
b=Container[beginer];
cout<<Container[beginer]<<endl;
beginer++;
}
return b;
}
int Queue:: front ()
{
int b=0;
if (beginer>=ender)
cout<<"error"<<endl;
else{
b=Container[beginer];
cout<<Container[beginer]<<endl;
}
return b;
}
int main() {
int a, b;
Queue q;
cin >>a;
string In;
while (a--) {
cin >>In;
switch (In[1])
{
case 'u':
cin>>b;
q.push(b);
break;
case 'o':
q.pop();
break;
case 'r':
q.front();
break;
default:
break;
}
}
}
// 64 位输出请用 printf("%lld")