#include
#include
#include
//seq type of queue
typedef struct rotative_queue
{
int *data;
int rear;
int front;
int length;
int n;
}rqueue,*prqueue;
bool push(prqueue q,int x)
{
q->data[q->rear] = x;
q->rear = (q->rear + 1)%(q->n);
q->length ++;
return 1;
}
int pop(prqueue q)
{
if(!q->length) return -1;
int x = q->data[q->front];
q->front = (q->front+1)%(q->n);
q->length --;
return x;
}
bool isEmpty(prqueue q)
{
if(!q->length) return 1;
else return 0;
}
bool isFull(prqueue q)
{
if(q->length==q->n) return 1;
else return 0;
}
int top(prqueue q)
{
return q->data[q->front];
}
prqueue createQueue()
{
prqueue q = (prqueue)malloc(sizeof(rqueue));
q->front = 0;
q->rear = 0;
q->length = 0;
return q;
}
void printqueue(prqueue q)
{
if(q->front>=q->rear) q->rear +=q->n;
for(int i=0;ilength;i++)
{
printf("%d ",q->data[(q->front+i)%q->n] );
}
printf("\n");
}
int main()
{
int n;
scanf("%d",&n);
getchar();
prqueue q = createQueue();
q->n = n;
q->data = (int*)malloc(sizeof(int)*n);
while(1)
{
char x= getchar();
if(x==' ') continue;
else if(x=='\n') break;
if(!isFull(q))
push(q,(x-'0'));
else break;
}
char s[4];
scanf("%s",s);
int tmp;
scanf("%d",&tmp);
while(pop(q)!=tmp);
int x=top(q);
printqueue(q);
printf("%d\n",x);
return 0;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)