168 lines
3.1 KiB
C
168 lines
3.1 KiB
C
#include<stdio.h>
|
||
#include<stdlib.h>
|
||
#include<conio.h>
|
||
#define MAX 1000
|
||
typedef int DataType;
|
||
typedef struct {
|
||
DataType data[MAX]; // 元素
|
||
int front,rear; // 队头队尾指针
|
||
}SqQueue;
|
||
|
||
void InitQueue(SqQueue *sq){
|
||
sq->rear = sq->front = 0;
|
||
printf("\n初始化成功\n");
|
||
}
|
||
int EnQueue(SqQueue *sq, DataType e){
|
||
if((sq->rear+1) % MAX == sq->front){
|
||
return 0;
|
||
}
|
||
sq->rear = (sq->rear + 1) % MAX;
|
||
sq->data[sq->rear] = e;
|
||
return 1;
|
||
}
|
||
int DeQueue(SqQueue *sq, DataType *e){
|
||
if(sq->rear == sq->front){
|
||
return 0;
|
||
}
|
||
sq->front = (sq->front+1) % MAX; // 队头循环进 1
|
||
*e = sq->data[sq->front];
|
||
return 1;
|
||
}
|
||
int GetHead(SqQueue *sq, DataType *e){
|
||
if(sq->rear == sq->front){
|
||
return 0;
|
||
}
|
||
*e = sq->data[(sq->front + 1) % MAX];
|
||
return 1;
|
||
}
|
||
int QueueEmpty(SqQueue sq){
|
||
if(sq.rear == sq.front){
|
||
return 1;
|
||
}
|
||
return 0;
|
||
}
|
||
int QueueFull(SqQueue sq){
|
||
if((sq.rear+1) % MAX == sq.front){
|
||
return 1;
|
||
}
|
||
return 0;
|
||
}
|
||
void OutputQueue(SqQueue sq){
|
||
DataType e;
|
||
if(sq.rear == sq.front){
|
||
return;
|
||
}
|
||
while(sq.rear != sq.front){
|
||
DeQueue(&sq,&e);
|
||
printf("%d ", e);
|
||
}
|
||
printf("\n");
|
||
}
|
||
int DeleteOne(SqQueue *sq){
|
||
DataType e;
|
||
if(sq->rear == sq->front){
|
||
return 0;
|
||
}
|
||
DeQueue(sq,&e);
|
||
return 1;
|
||
}
|
||
void baoshu(SqQueue *sq, int n, int e){
|
||
SqQueue Q2;
|
||
InitQueue(&Q2);
|
||
DataType temp;
|
||
int i=0;
|
||
for(int i=1;i<=n;i++){
|
||
EnQueue(sq, i); // 全部入队
|
||
}
|
||
while(!QueueEmpty(*sq)){
|
||
i++;
|
||
DeQueue(sq,&temp);
|
||
if(i==e){
|
||
EnQueue(&Q2,temp); // 出队顺序
|
||
i=0;
|
||
} else {
|
||
EnQueue(sq,temp); // 重新入队
|
||
}
|
||
}
|
||
printf("出队顺序如下:\n");
|
||
OutputQueue(Q2);
|
||
}
|
||
int main(){
|
||
SqQueue Q1;
|
||
int n,choice;
|
||
DataType e;
|
||
InitQueue(&Q1); // 初始化
|
||
|
||
for(;;){
|
||
printf("\t\t1.入队\n");
|
||
printf("\t\t2.出队\n");
|
||
printf("\t\t3.输出所有元素\n");
|
||
printf("\t\t4.读取队列的第一个元素\n");
|
||
printf("\t\t5.删除一个元素\n");
|
||
printf("\t\t6.应用题:重复数数\n");
|
||
printf("\t\t0.退出\n");
|
||
|
||
printf("请选择:");
|
||
scanf("%d",&choice);
|
||
if(choice==0) break;
|
||
switch (choice) {
|
||
case 1:
|
||
printf("请输入需要输入的元素个数:");
|
||
scanf("%d",&n);
|
||
for(int i=0;i<n;i++){
|
||
printf("请输入第 %d 个元素:",i+1);
|
||
scanf("%d",&e);
|
||
EnQueue(&Q1,e);
|
||
}
|
||
OutputQueue(Q1);
|
||
break;
|
||
case 2:
|
||
n=DeQueue(&Q1,&e);
|
||
if(n>0){
|
||
printf("出队元素为 %d\n",e);
|
||
} else {
|
||
printf("队为空\n");
|
||
}
|
||
break;
|
||
case 3:
|
||
OutputQueue(Q1);
|
||
break;
|
||
case 4:
|
||
n=GetHead(&Q1,&e);
|
||
if(n>0){
|
||
printf("队列的第一个元素为 %d\n",e);
|
||
} else {
|
||
printf("队为空\n");
|
||
}
|
||
break;
|
||
case 5:
|
||
n=DeleteOne(&Q1);
|
||
if(n==0){
|
||
printf("删除失败,队为空。\n");
|
||
} else {
|
||
printf("删除成功\n");
|
||
}
|
||
OutputQueue(Q1);
|
||
break;
|
||
case 6:
|
||
InitQueue(&Q1); // 重新初始化
|
||
printf("请输入人数:");
|
||
scanf("%d",&n);
|
||
if(n>MAX-1){
|
||
printf("人数过多,请重试");
|
||
break;
|
||
}
|
||
printf("请输入指定的报数 m:");
|
||
scanf("%d",&e);
|
||
baoshu(&Q1,n,e);
|
||
default:
|
||
break;
|
||
}
|
||
printf("请按任意键继续...");
|
||
getch();
|
||
system("cls");
|
||
}
|
||
printf("感谢使用,再见\n");
|
||
return 0;
|
||
}
|