A-C/ex3_2_1.c

212 lines
3.5 KiB
C
Raw Normal View History

2022-04-15 01:51:45 +00:00
/*
Des: <EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ֻ<EFBFBD><EFBFBD>ζ<EFBFBD><EFBFBD>еĻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define MAX 1000
typedef int DataType;
typedef struct {
DataType data[MAX]; // Ԫ<><D4AA>
int front,rear; // <20><>ͷ<EFBFBD><CDB7>βָ<CEB2><D6B8>
}SqQueue;
/* <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
void InitQueue(SqQueue *sq){
sq->rear = sq->front = 0;
printf("\n<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>\n");
}
/*
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> e <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ 1 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 0 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
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;
}
/*
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD>ӵ<EFBFBD> e
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ 1 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 0 <EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>
*/
int DeQueue(SqQueue *sq, DataType *e){
if(sq->rear == sq->front){
return 0;
}
sq->front = (sq->front+1) % MAX;
*e = sq->data[sq->front];
return 1;
}
/*
<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>ͷԪ<EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD> e
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ 1 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 0 <EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>
*/
int GetHead(SqQueue *sq, DataType *e){
if(sq->rear == sq->front){
return 0;
}
*e = sq->data[(sq->front + 1) % MAX];
return 1;
}
/*
<EFBFBD>ж϶<EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ 0 <EFBFBD><EFBFBD><EFBFBD>Ӳ<EFBFBD><EFBFBD>գ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ 1 <EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>
*/
int QueueEmpty(SqQueue sq){
if(sq.rear == sq.front){
return 1;
}
return 0;
}
/*
<EFBFBD>ж϶<EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ 0 <EFBFBD><EFBFBD><EFBFBD>Ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ 1 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
int QueueFull(SqQueue sq){
if((sq.rear+1) % MAX == sq.front){
return 1;
}
return 0;
}
/*
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>
*/
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");
}
/*
<EFBFBD>Ӷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ 1<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ 0 <EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>
*/
int DeleteOne(SqQueue *sq){
DataType e;
if(sq->rear == sq->front){
return 0;
}
DeQueue(sq,&e);
return 1;
}
/*
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD>Ƚ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>sq<EFBFBD><EFBFBD>ѭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>sq<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>ֵ<EFBFBD>򱣴<EFBFBD>µĶ<EFBFBD><EFBFBD><EFBFBD>Q2<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>sq<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>µĶ<EFBFBD><EFBFBD><EFBFBD>Q2<EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><EFBFBD>
n Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>e Ϊָ<EFBFBD><EFBFBD>ֵ
*/
void chulie(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("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>\n");
OutputQueue(Q2);
}
int main(){
SqQueue Q1;
int n,choice;
DataType e;
InitQueue(&Q1);
for(;;){
printf("\t\t1.<2E><><EFBFBD><EFBFBD>\n");
printf("\t\t2.<2E><><EFBFBD><EFBFBD>\n");
printf("\t\t3.<2E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>\n");
printf("\t\t4.<2E><>ȡ<EFBFBD><C8A1><EFBFBD>еĵ<D0B5>һ<EFBFBD><D2BB>Ԫ<EFBFBD><D4AA>\n");
printf("\t\t5.ɾ<><C9BE>һ<EFBFBD><D2BB>Ԫ<EFBFBD><D4AA>\n");
printf("\t\t6.Ӧ<><D3A6><EFBFBD><EFBFBD><E2A3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
printf("\t\t0.<2E>˳<EFBFBD>\n");
printf("<EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
scanf("%d",&choice);
if(choice==0) break;
switch (choice) {
case 1:
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ظ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
scanf("%d",&n);
for(int i=0;i<n;i++){
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> %d <20><>Ԫ<EFBFBD>أ<EFBFBD>",i+1);
scanf("%d",&e);
EnQueue(&Q1,e);
}
OutputQueue(Q1);
break;
case 2:
n=DeQueue(&Q1,&e);
if(n>0){
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>Ϊ %d\n",e);
} else {
printf("<EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>\n");
}
break;
case 3:
OutputQueue(Q1);
break;
case 4:
n=GetHead(&Q1,&e);
if(n>0){
printf("<EFBFBD><EFBFBD><EFBFBD>еĵ<EFBFBD>һ<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>Ϊ %d\n",e);
} else {
printf("<EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>\n");
}
break;
case 5:
n=DeleteOne(&Q1);
if(n==0){
printf("ɾ<EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>ա<EFBFBD>\n");
} else {
printf("ɾ<EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>\n");
}
OutputQueue(Q1);
break;
case 6:
InitQueue(&Q1);
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
scanf("%d",&n);
while(n>MAX-1){
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
scanf("%d",&n);
}
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> m<><6D>");
scanf("%d",&e);
chulie(&Q1,n,e);
default:
break;
}
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>...");
getch();
system("cls");
}
printf("<EFBFBD><EFBFBD>лʹ<EFBFBD>ã<EFBFBD><EFBFBD>ټ<EFBFBD>\n");
return 0;
}