手动模拟队列(队头指针和队尾指针都从0开始)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5+10;
int q[N];
int n;
int hh,tt;
int main()
{
scanf("%d",&n);
while (n--)
{
char s[6];
scanf("%s",s);
if (s[0]=='p'&&s[1]=='u')
{
int x;
scanf("%d",&x);
q[tt++]=x;
}
else if (s[0]=='p'&&s[1]=='o')
{
if (hh!=tt)
{
hh++;
}
}
else if (s[0]=='e')
{
if (hh<tt) printf("NO\n");
else printf("YES\n");
}
else
{
printf("%d\n",q[hh]);
}
}
return 0;
}
手动模拟队列(头指针从0开始,尾指针从-1开始)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5+10;
int q[N];
int n;
int main()
{
scanf("%d",&n);
int hh=0,tt=-1;
while (n--)
{
char s[6];
scanf("%s",s);
if (s[0]=='p'&&s[1]=='u')
{
int x;
scanf("%d",&x);
q[++tt]=x;
}
else if (s[0]=='p'&&s[1]=='o')
{
if (hh<=tt)
{
hh++;
}
}
else if (s[0]=='e')
{
if (hh<=tt) printf("NO\n");
else printf("YES\n");
}
else
{
printf("%d\n",q[hh]);
}
}
return 0;
}
STL的队列直接实现,不模拟了(狗头)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int m;
int main()
{
queue<int>q;
scanf("%d",&m);
while (m--)
{
char s[6];
scanf("%s",s);
if (s[0]=='p'&&s[1]=='u')
{
int x;
scanf("%d",&x);
q.push(x);
}
else if (s[0]=='p'&&s[1]=='o')
{
if (!q.empty())
{
q.pop();
}
}
else if (s[0]=='e')
{
if (q.empty())
{
printf("YES\n");
}
else printf("NO\n");
}
else
{
printf("%d\n",q.front());
}
}
return 0;
}