From d900c474096babd93133c9f9f3e908158617eb3c Mon Sep 17 00:00:00 2001 From: xtaodada Date: Thu, 14 Apr 2022 21:52:16 -0400 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20'ex3=5F2=5F2.c'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ex3_2_2.c | 315 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 173 insertions(+), 142 deletions(-) diff --git a/ex3_2_2.c b/ex3_2_2.c index 7ff48b7..f84d624 100644 --- a/ex3_2_2.c +++ b/ex3_2_2.c @@ -1,142 +1,173 @@ -#include -#include -#include -typedef int DataType; -typedef struct QNode{ - DataType data; // 元素 - struct QNode *next; // 指向下一个结点的指针 -}QType; -typedef struct { - QType *front,*rear; // 队头队尾指针 -}LinkQueue; - -void InitQueue(LinkQueue *lq){ - lq=(LinkQueue *)malloc(sizeof(LinkQueue)); - lq->rear=lq->front=NULL; - printf("\n初始化成功\n"); -} -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; -} -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; -} -int GetHead(LinkQueue *lq, DataType *e){ - if(lq->front==NULL){ - return 0; - } - *e = lq->front->data; - return 1; -} -int QueueEmpty(LinkQueue *lq){ - if(lq->front==NULL) return 1; - return 0; -} -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"); -} -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.入队\n"); - printf("\t\t2.出队\n"); - printf("\t\t3.输出所有元素\n"); - printf("\t\t4.读取队列的第一个元素\n"); - printf("\t\t5.删除一个元素\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;i0){ - 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; - default: - break; - } - printf("请按任意键继续..."); - getch(); - system("cls"); - } - printf("感谢使用,再见\n"); - return 0; -} +/* + Des: 此文件用于实现链式队列的基本操作 +*/ +#include +#include +#include +typedef int DataType; +typedef struct QNode{ + DataType data; // 元素 + struct QNode *next; // 指向下一个结点的指针 +}QType; +typedef struct { + QType *front,*rear; // 队头队尾指针 +}LinkQueue; +/* 初始化队列 */ +void InitQueue(LinkQueue *lq){ + lq=(LinkQueue *)malloc(sizeof(LinkQueue)); + lq->rear=lq->front=NULL; + printf("\n初始化成功\n"); +} +/* + 将数据 e 入队 + + 返回值恒为 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; +} +/* + 将数据出队到 e + + 返回值为 1 ,如果返回 0 则队空 +*/ +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; +} +/* + 读取队头元素值给 e + + 返回值为 1 ,如果返回 0 则队空 +*/ +int GetHead(LinkQueue *lq, DataType *e){ + if(lq->front==NULL){ + return 0; + } + *e = lq->front->data; + return 1; +} +/* + 判断队是否为空 + + 返回值为 0 则队不空,返回值为 1 则队空 +*/ +int QueueEmpty(LinkQueue *lq){ + if(lq->front==NULL) return 1; + return 0; +} +/* + 输出队列中的所有元素 +*/ +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"); +} +/* + 从队列中删除一个元素 + + 返回值为 1,如果返回值为 0 则队空 +*/ +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.入队\n"); + printf("\t\t2.出队\n"); + printf("\t\t3.输出所有元素\n"); + printf("\t\t4.读取队列的第一个元素\n"); + printf("\t\t5.删除一个元素\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;i0){ + 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; + default: + break; + } + printf("请按任意键继续..."); + getch(); + system("cls"); + } + printf("感谢使用,再见\n"); + return 0; +} \ No newline at end of file