第七周 项目2 - 建立链队算法库

本文展示了一个使用C语言实现的链式队列的基本操作示例,包括初始化、入队、出队等,并通过具体代码演示了如何进行链队列的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题及代码:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2. #include "liqueue.h"  
  3.   
  4. int main()  
  5. {  
  6.     ElemType e;  
  7.     LiQueue *q;  
  8.     printf("(1)初始化链队q\n");  
  9.     InitQueue(q);  
  10.     printf("(2)依次进链队元素a,b,c\n");  
  11.     enQueue(q,'a');  
  12.     enQueue(q,'b');  
  13.     enQueue(q,'c');  
  14.     printf("(3)链队为%s\n",(QueueEmpty(q)?"空":"非空"));  
  15.     if (deQueue(q,e)==0)  
  16.         printf("队空,不能出队\n");  
  17.     else  
  18.         printf("(4)出队一个元素%c\n",e);  
  19.     printf("(5)链队q的元素个数:%d\n",QueueLength(q));  
  20.     printf("(6)依次进链队元素d,e,f\n");  
  21.     enQueue(q,'d');  
  22.     enQueue(q,'e');  
  23.     enQueue(q,'f');  
  24.     printf("(7)链队q的元素个数:%d\n",QueueLength(q));  
  25.     printf("(8)出链队序列:");  
  26.     while (!QueueEmpty(q))  
  27.     {  
  28.         deQueue(q,e);  
  29.         printf("%c ",e);  
  30.     }  
  31.     printf("\n");  
  32.     printf("(9)释放链队\n");  
  33.     DestroyQueue(q);  
  34.     return 0;  
  35. }  
 

liqueue.h

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /*              
  2. Copyright (c)2016,烟台大学计算机与控制工程学院              
  3. All rights reserved.              
  4. 文件名称:建立链队算法库.cpp              
  5. 作    者:  陈朋        
  6. 完成日期:2016年10月13日              
  7. 版 本 号:v1.0                 
  8. 问题描述:         
  9. 输入描述:无             
  10. 程序输出:若干输出。           
  11. */       
  12.     
  13. #ifndef LIQUEUE_H_INCLUDED  
  14. #define LIQUEUE_H_INCLUDED  
  15.   
  16. typedef char ElemType;  
  17. typedef struct qnode  
  18. {  
  19.     ElemType data;  
  20.     struct qnode *next;  
  21. } QNode;        //链队数据结点类型定义  
  22.   
  23. typedef struct  
  24. {  
  25.     QNode *front;  
  26.     QNode *rear;  
  27. } LiQueue;          //链队类型定义  
  28. void InitQueue(LiQueue *&q);  //初始化链队  
  29. void DestroyQueue(LiQueue *&q);  //销毁链队  
  30. bool QueueEmpty(LiQueue *q);  //判断链队是否为空  
  31. int QueueLength(LiQueue *q);  //返回队列中数据元素个数  
  32. void enQueue(LiQueue *&q,ElemType e);  //入队  
  33. bool deQueue(LiQueue *&q,ElemType &e);   //出队  
  34.   
  35. #endif // LIQUEUE_H_INCLUDED  

liqueue.cpp

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include "liqueue.h"  
  4.   
  5. void InitQueue(LiQueue *&q)  //初始化链队  
  6. {  
  7.     q=(LiQueue *)malloc(sizeof(LiQueue));  
  8.     q->front=q->rear=NULL;  
  9. }  
  10. void DestroyQueue(LiQueue *&q)  //销毁链队  
  11. {  
  12.     QNode *p=q->front,*r;   //p指向队头数据节点  
  13.     if (p!=NULL)            //释放数据节点占用空间  
  14.     {  
  15.         r=p->next;  
  16.         while (r!=NULL)  
  17.         {  
  18.             free(p);  
  19.             p=r;  
  20.             r=p->next;  
  21.         }  
  22.     }  
  23.     free(p);  
  24.     free(q);                //释放链队节点占用空间  
  25. }  
  26. bool QueueEmpty(LiQueue *q)  //判断链队是否为空  
  27. {  
  28.     return(q->rear==NULL);  
  29. }  
  30. int QueueLength(LiQueue *q)  //返回队列中数据元素个数  
  31. {  
  32.     int n=0;  
  33.     QNode *p=q->front;  
  34.     while (p!=NULL)  
  35.     {  
  36.         n++;  
  37.         p=p->next;  
  38.     }  
  39.     return(n);  
  40. }  
  41. void enQueue(LiQueue *&q,ElemType e)  //入队  
  42. {  
  43.     QNode *p;  
  44.     p=(QNode *)malloc(sizeof(QNode));  
  45.     p->data=e;  
  46.     p->next=NULL;  
  47.     if (q->rear==NULL)      //若链队为空,则新节点是队首节点又是队尾节点  
  48.         q->front=q->rear=p;  
  49.     else  
  50.     {  
  51.         q->rear->next=p;    //将*p节点链到队尾,并将rear指向它  
  52.         q->rear=p;  
  53.     }  
  54. }  
  55. bool deQueue(LiQueue *&q,ElemType &e)   //出队  
  56. {  
  57.     QNode *t;  
  58.     if (q->rear==NULL)      //队列为空  
  59.         return false;  
  60.     t=q->front;             //t指向第一个数据节点  
  61.     if (q->front==q->rear)  //队列中只有一个节点时  
  62.         q->front=q->rear=NULL;  
  63.     else                    //队列中有多个节点时  
  64.         q->front=q->front->next;  
  65.     e=t->data;  
  66.     free(t);  
  67.     return true;  
  68. }  
  69. 运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值