有 先进先出(队列) 和 先进后出 (栈)两种情况。
#include <stack>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
int main()
{
int n,m,num;
char ch[5],st[5];
scanf("%d",&n);
while (n--)
{
scanf("%d",&m);
scanf("%s",ch);
if (strcmp(ch,"FILO")==0)
{
stack <int> FILO;
while (m--)
{
scanf("%s",st);
if (strcmp(st,"IN")==0)
{
scanf("%d",&num);
FILO.push(num);
}
else if (strcmp(st,"OUT")==0)
{
if (FILO.empty())
{
printf("None\n");
}
else
{
printf("%d\n",FILO.top());
FILO.pop();
}
}
}
}
else
{
queue <int> FIFO;
while (m--)
{
scanf("%s",st);
if (strcmp(st,"IN")==0)
{
scanf("%d",&num);
FIFO.push(num);
}
else if(strcmp(st,"OUT")==0)
{
if (FIFO.empty())
{
printf("None\n");
}
else
{
printf("%d\n",FIFO.front());
FIFO.pop();
}
}
}
}
}
return 0;
}