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

  1.  *Copyright(c)2016,烟台大学计算机与控制工程学院  
  2.  *All right reserved.  
  3.  *文件名称:链队算法库.cpp  
  4.  *作者:陈晓琳  
  5.  *完成日期;2016年10月13日  
  6.  *版本号;v1.0  
  7.  *  
  8.  *问题描述: 建立链队算法库。  
  9.   
  10.  *输入描述:队列的操作  
  11.  *程序输出:队列操作后的结果  
  12. */  
  13.   
  14.   
  15. #include <stdio.h>  
  16. #include "liqueue.h"  
  17.   
  18. int main()  
  19. {  
  20.     ElemType e;  
  21.     LiQueue *q;  
  22.     printf("(1)初始化链队q\n");  
  23.     InitQueue(q);  
  24.     printf("(2)依次进链队元素a,b,c\n");  
  25.     enQueue(q,'a');  
  26.     enQueue(q,'b');  
  27.     enQueue(q,'c');  
  28.     printf("(3)链队为%s\n",(QueueEmpty(q)?"空":"非空"));  
  29.     if (deQueue(q,e)==0)  
  30.         printf("队空,不能出队\n");  
  31.     else  
  32.         printf("(4)出队一个元素%c\n",e);  
  33.     printf("(5)链队q的元素个数:%d\n",QueueLength(q));  
  34.     printf("(6)依次进链队元素d,e,f\n");  
  35.     enQueue(q,'d');  
  36.     enQueue(q,'e');  
  37.     enQueue(q,'f');  
  38.     printf("(7)链队q的元素个数:%d\n",QueueLength(q));  
  39.     printf("(8)出链队序列:");  
  40.     while (!QueueEmpty(q))  
  41.     {  
  42.         deQueue(q,e);  
  43.         printf("%c ",e);  
  44.     }  
  45.     printf("\n");  
  46.     printf("(9)释放链队\n");  
  47.     DestroyQueue(q);  
  48.     return 0;  
  49. }  
  50. #include <stdio.h>  
  51. #include <malloc.h>  
  52. #include "liqueue.h"  
  53.   
  54. void InitQueue(LiQueue *&q)  //初始化链队  
  55. {  
  56.     q=(LiQueue *)malloc(sizeof(LiQueue));  
  57.     q->front=q->rear=NULL;  
  58. }  
  59. void DestroyQueue(LiQueue *&q)  //销毁链队  
  60. {  
  61.     QNode *p=q->front,*r;   //p指向队头数据节点  
  62.     if (p!=NULL)            //释放数据节点占用空间  
  63.     {  
  64.         r=p->next;  
  65.         while (r!=NULL)  
  66.         {  
  67.             free(p);  
  68.             p=r;  
  69.             r=p->next;  
  70.         }  
  71.     }  
  72.     free(p);  
  73.     free(q);                //释放链队节点占用空间  
  74. }  
  75. bool QueueEmpty(LiQueue *q)  //判断链队是否为空  
  76. {  
  77.     return(q->rear==NULL);  
  78. }  
  79. int QueueLength(LiQueue *q)  //返回队列中数据元素个数  
  80. {  
  81.     int n=0;  
  82.     QNode *p=q->front;  
  83.     while (p!=NULL)  
  84.     {  
  85.         n++;  
  86.         p=p->next;  
  87.     }  
  88.     return(n);  
  89. }  
  90. void enQueue(LiQueue *&q,ElemType e)  //入队  
  91. {  
  92.     QNode *p;  
  93.     p=(QNode *)malloc(sizeof(QNode));  
  94.     p->data=e;  
  95.     p->next=NULL;  
  96.     if (q->rear==NULL)      //若链队为空,则新节点是队首节点又是队尾节点  
  97.         q->front=q->rear=p;  
  98.     else  
  99.     {  
  100.         q->rear->next=p;    //将*p节点链到队尾,并将rear指向它  
  101.         q->rear=p;  
  102.     }  
  103. }  
  104. bool deQueue(LiQueue *&q,ElemType &e)   //出队  
  105. {  
  106.     QNode *t;  
  107.     if (q->rear==NULL)      //队列为空  
  108.         return false;  
  109.     t=q->front;             //t指向第一个数据节点  
  110.     if (q->front==q->rear)  //队列中只有一个节点时  
  111.         q->front=q->rear=NULL;  
  112.     else                    //队列中有多个节点时  
  113.         q->front=q->front->next;  
  114.     e=t->data;  
  115.     free(t);  
  116.     return true;  
  117. }  
  118.   
  119. #ifndef LIQUEUE_H_INCLUDED  
  120. #define LIQUEUE_H_INCLUDED  
  121.   
  122.   
  123. typedef char ElemType;  
  124. typedef struct qnode  
  125. {  
  126.     ElemType data;  
  127.     struct qnode *next;  
  128. } QNode;        //链队数据结点类型定义  
  129.   
  130. typedef struct  
  131. {  
  132.     QNode *front;  
  133.     QNode *rear;  
  134. } LiQueue;          //链队类型定义  
  135. void InitQueue(LiQueue *&q);  //初始化链队  
  136. void DestroyQueue(LiQueue *&q);  //销毁链队  
  137. bool QueueEmpty(LiQueue *q);  //判断链队是否为空  
  138. int QueueLength(LiQueue *q);  //返回队列中数据元素个数  
  139. void enQueue(LiQueue *&q,ElemType e);  //入队  
  140. bool deQueue(LiQueue *&q,ElemType &e);   //出队  
  141.   
  142.   
  143. #endif // LIQUEUE_H_INCLUDED  


运行结果:


知识点总结:

定义链队存储结构,实现其基本运算。

学习心得:

基础知识很重要,要掌握牢固。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值