考虑的不是很周到,有一个地方需要注意:当队列为空的时候,是不能再将队首移除的。
题目如下:
第1行有一个整数n 接下来n行 I表示要插入一个数 O表示将队首的数出队 | |||
输出格式: | 输出队列当中最后留下的数 | ||
限制: | 50% 1<=n<=10000 100% 1<=n<=100000 | ||
样例: |
|
代码如下:
/*队列的运用*/
#include <stdio.h>
struct queue
{
int data[100005];
int head;
int tail;
};
int main()
{
int n,t;
char str;
struct queue q;
q.head = 0;
q.tail = 0;
scanf("%d",&n);
for(int i = 0;i < n;i++)
{
getchar();
scanf("%c",&str);
if(str=='I')
{
scanf("%d",&t);
q.data[q.tail++] = t;
}
else if(str=='O'&&q.head<q.tail)
{
q.head++;
}
}
while(q.head<q.tail)
{
printf("%d ",q.data[q.head++]);
}
return 0;
}