代码改变世界

利用队列实现任意数到几选择第几个出队列

2013-05-20 17:28  夏洛克·福尔摩斯  阅读(212)  评论(0编辑  收藏  举报

#include<stdio.h>
#include<stdlib.h>
#define N 20

typedef struct
{
int a[N];
int front,rear;
}queue;

int main()
{
queue *s=(queue *)malloc(sizeof(queue));
s->front=s->rear=0;


printf("请输入队列中的个数(输入小于20的数):");
int i;
scanf("%d",&i);
getchar();
int j=0;
for(j=0;j<i;j++)
{
scanf("%d",&(s->a[j]));//初始化队列
s->a[s->rear]=s->a[j];
s->rear=(s->rear+1)%i;
}
int g;
printf("请选择数到几:");//空几个数
scanf("%d",&g);
printf("请选择第几个出队列:");//front执行他后面,rear指向front的后面
int t;
scanf("%d",&t);
s->rear=t-2;
s->front=t-1;
while(s->front!=s->rear)
{

int k=0;
printf("%d->",s->a[s->front]);
s->front=(s->front+1)%i;

for(;k<g-1;k++)//空几个就替换几次,实现队列的重新排序
{
s->rear=(s->rear+1)%i;
s->a[s->rear]=s->a[s->front];
s->front=(s->front+1)%i;
}
}
printf("%d\n",s->a[s->front]);
free(s);//释放
return 0;
}