将两根筷子平行的放在一起,就构成了一个队列。将带有编号的鸡蛋放到两根筷子之间叫做入队(push),将筷子之间的鸡蛋拿出来叫做出队(pop)。但这两种方式有特殊的定义,对于入队,只能将鸡蛋从队列的尾部向里放入;对于出队,只能将鸡蛋从队列的头部向外将鸡蛋拿出来。
将①、②入队:
头____________尾 _____________ ______________
← ① ← ② ① ← ② ① ②
______________ _____________ ______________
将①、②出队:
头____________尾 ______________ ______________
← ① ← ② ① ← ② ① ②
______________ ______________ ______________
输入
第二行输入一个数N,表示有N(N<=10)种操作
接下来N行,每行一种操作,push表示将编号为x的鸡蛋放入队列中,pop表示拿走队列头部的一个鸡蛋。
数据输入保证合法,队列中没有鸡蛋时不会有出队操作!
输出
样例输入
2 3 push 1 push 2 push 3 2 push 1 pop
样例输出
1 2 3 no eggs!
#include<stdio.h> #include<string.h> int main(){ int front,end,m,n,num,i,a[11010]; char s1[5],s2[]="push",s3[]="pop"; scanf("%d",&m); while(m--){ front=end=0; scanf("%d",&n); while(n--){ scanf("%s",s1); if(strcmp(s1,s2)==0){ scanf("%d",&num); a[end++]=num; } else if(strcmp(s1,s3)==0) a[front++]; } if(front==end) printf("no eggs!\n"); else{ for(i=front;i<end;i++) printf("%d ",a[i]); printf("\n"); } } return 0; }