第七周--项目1建立环形队列算法库

来源:互联网 发布:js 判断function 编辑:程序博客网 时间:2024/06/06 05:10
  1. *           
  2.  * Copyright (c++) 2016 烟台大学计算机学院           
  3.  * All right reserved.           
  4.  * 文件名称:huan.cpp           
  5.  * 作    者:陈晓琳           
  6.  * 完成日期:2016年10月13日           
  7.  * 版 本 号:v1.9            
  8.  *           
  9.  *问题描述:建立环形队列算法库  
  10.     
  11. *           

sqqueue.h

[cpp] view plain copy
  1. #ifndef SQQUEUE_H_INCLUDED  
  2. #define SQQUEUE_H_INCLUDED  
  3. #include <stdio.h>  
  4. #include <malloc.h>  
  5. #define MaxSize 5  
  6. typedef char ElemType;  
  7. typedef struct  
  8. {  
  9.     ElemType data[MaxSize];  
  10.     int front,rear;     /*队首和队尾指针*/  
  11. } SqQueue;  
  12.   
  13.   
  14. void InitQueue(SqQueue *&q);  //初始化顺序环形队列  
  15. void DestroyQueue(SqQueue *&q); //销毁顺序环形队列  
  16. bool QueueEmpty(SqQueue *q);  //判断顺序环形队列是否为空  
  17. int QueueLength(SqQueue *q);   //返回队列中元素个数,也称队列长度  
  18. bool enQueue(SqQueue *&q,ElemType e);   //进队  
  19. bool deQueue(SqQueue *&q,ElemType &e);  //出队  
  20.   
  21. #endif // SQQUEUE_H_INCLUDED  


sqqueue.cpp

[cpp] view plain copy
  1. #include "sqqueue.h"  
  2.   
  3. void InitQueue(SqQueue *&q)  //初始化顺序环形队列  
  4. {  
  5.     q=(SqQueue *)malloc (sizeof(SqQueue));  
  6.     q->front=q->rear=0;   //队首队尾指针相等 队列为空  
  7. }  
  8. void DestroyQueue(SqQueue *&q) //销毁顺序环形队列  
  9. {  
  10.     free(q);  
  11. }  
  12. bool QueueEmpty(SqQueue *q)  //判断顺序环形队列是否为空  
  13. {  
  14.     return(q->front==q->rear);  
  15. }  
  16.   
  17.   
  18. int QueueLength(SqQueue *q)   //返回队列中元素个数,也称队列长度  
  19. {  
  20.     return (q->rear-q->front+MaxSize)%MaxSize;  
  21. }  
  22.   
  23. bool enQueue(SqQueue *&q,ElemType e)   //进队  
  24. {  
  25.     if ((q->rear+1)%MaxSize==q->front)  //队满上溢出  
  26.         return false;  
  27.     q->rear=(q->rear+1)%MaxSize;   //队尾指针后移  
  28.     q->data[q->rear]=e;  
  29.     return true;  
  30. }  
  31. bool deQueue(SqQueue *&q,ElemType &e)  //出队  
  32. {  
  33.     if (q->front==q->rear)      //队空下溢出  
  34.         return false;  
  35.     q->front=(q->front+1)%MaxSize;    //队首插入  
  36.     e=q->data[q->front];  
  37.     return true;  
  38. }  


main.pp

[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include "sqqueue.h"  
  3.   
  4. int main()  
  5. {  
  6.     ElemType e;  
  7.     SqQueue *q;  
  8.     printf("(1)初始化队列q\n");  
  9.     InitQueue(q);  
  10.     printf("(2)依次进队列元素a,b,c\n");  
  11.     if (enQueue(q,'a')==0) printf("队满,不能进队\n");  
  12.     if (enQueue(q,'b')==0) printf("队满,不能进队\n");  
  13.     if (enQueue(q,'c')==0) printf("队满,不能进队\n");  
  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.     if (enQueue(q,'d')==0) printf("队满,不能进队\n");  
  22.     if (enQueue(q,'e')==0) printf("队满,不能进队\n");  
  23.     if (enQueue(q,'f')==0) printf("队满,不能进队\n");  
  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. }  
  36. 运行结果:

知识点:环形队列的存储及基本操作。
心得:环形队列仍具有队列的特征,队尾进,队首出,首尾相等队为空。插入和删除时的求余体现了环形队列循环,在纸上画出后更容易理解算法语句。
0 0
原创粉丝点击