#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
typedef struct NODE{
int n;
struct NODE* next;
}NODE;
typedef struct Queue{
struct NODE* front;
struct NODE* rear;
}Queue;
Queue* CreateEmptyQueue(){
Queue* qptr=(Queue*)malloc(sizeof(Queue));
if(qptr!=NULL){
qptr->front=qptr->rear=NULL;
}
else{
printf("Out of space!\n");
}
return qptr;
}
int IsQueueEmpty(Queue* qptr){
int flag=0;
if(qptr->front==NULL){
flag=1;
}
return flag;
}
void InsertQueue(Queue* qptr,int x){
NODE* p=(NODE*)malloc(sizeof(NODE));
if(p==NULL){
printf("Out of space!\n");
}
else{
p->n=x;
p->next=NULL;
if(IsQueueEmpty(qptr)){
qptr->front=qptr->rear=p;
}
else{
qptr->rear->next=p;
qptr->rear=p;
}
}
}
int DeleteQueue(Queue* qptr){
NODE* p;
int x;
if(qptr->front==NULL){
printf("Empty queue.\n");
return 0;
}
else{
p=qptr->front;
qptr->front=qptr->front->next;
qptr->rear->next=qptr->front;
x=p->n;
free(p);
return x;
}
}
int GetQueueValue(Queue* qptr){
int x=qptr->front->n;
return x;
}
int OutputQueueValue(Queue* qptr){
NODE* p;
p=qptr->front;
while(p->next!=qptr->front){
printf("%d ",p->n);
p=p->next;
}
printf("%d\n",p->n);
}
int main(){
int n;
scanf("%d",&n);
Queue* qptr=CreateEmptyQueue();
while(1){
int x;
scanf("%d",&x);
InsertQueue(qptr,x);
//用来处理输入一串数字之后的'\n'时,用下面这种方法:
char c=getchar();
if(c=='\n') break;
}
qptr->rear->next=qptr->front;
//构建循环队列
char s1[10];fgets(s1,8,stdin);
//应付"yes"输入
int v;
scanf("%d",&v);
for(;v!=qptr->front->n;DeleteQueue(qptr));
DeleteQueue(qptr);
OutputQueueValue(qptr);
printf("%d",GetQueueValue(qptr));
return 0;
}