A-C/ex3_2_2.c

173 lines
2.8 KiB
C
Raw Normal View History

2022-04-15 01:52:16 +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>
*/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef int DataType;
typedef struct QNode{
DataType data; // Ԫ<><D4AA>
struct QNode *next; // ָ<><D6B8><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>
}QType;
typedef struct {
QType *front,*rear; // <20><>ͷ<EFBFBD><CDB7>βָ<CEB2><D6B8>
}LinkQueue;
/* <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
void InitQueue(LinkQueue *lq){
lq=(LinkQueue *)malloc(sizeof(LinkQueue));
lq->rear=lq->front=NULL;
printf("\n<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>\n");
}
/*
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> e <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>Ϊ 1
*/
int EnQueue(LinkQueue *lq, DataType e){
QType *s;
s=(QType *)malloc(sizeof(QType));
s->data = e;
s->next = NULL;
if(lq->front==NULL){
lq->rear = lq->front = s;
} else {
lq->rear->next = s;
lq->rear = s;
}
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(LinkQueue *lq, DataType *e){
QType *p;
if(lq->front==NULL){
return 0;
}
p = lq->front;
*e = p->data;
if(lq->rear == lq->front){
lq->rear = lq->front = NULL;
} else {
lq->front = lq->front->next;
}
free(p);
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(LinkQueue *lq, DataType *e){
if(lq->front==NULL){
return 0;
}
*e = lq->front->data;
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(LinkQueue *lq){
if(lq->front==NULL) return 1;
return 0;
}
/*
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>
*/
void OutputQueue(LinkQueue *lq){
QType *p;
if(lq->front==NULL){
return;
}
p = lq->front;
while(p!=NULL){
printf("%d ", p->data);
p = p->next;
}
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(LinkQueue *lq){
DataType e;
if(QueueEmpty(lq)){
return 0;
}
DeQueue(lq,&e);
return 1;
}
int main(){
LinkQueue 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\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;
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;
}