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

  1. /* 
  2. Copyright (c)2016,烟台大学计算机与控制工程学院 
  3. All rights reserved. 
  4. 文件名称:wu.cbp 
  5. 作    者:武昊 
  6. 完成日期:2016年10月9日 
  7. 版 本 号:v1.0 
  8. 问题描述:定义顺序环形队列存储结构,实现其基本运算,并完成测试。 
  9.           要求:(1).头文件sqqueue.h中定义数据结构并声明用于完成基本运算的函数。 
  10.                 (2).在sqqueue.cpp中实现这些函数 
  11.                 (3).在main函数中完成测试 
  12. 输入描述:无 
  13. 程序输出:测试数据 
  14. */
  • sqqueue.h代码
  1. #ifndef SQQUEUE_H_INCLUDED  
  2. #define SQQUEUE_H_INCLUDED  
  3.   
  4.   
  5. #include <stdio.h>  
  6. #include <malloc.h>  
  7. #define MaxSize 5  
  8. typedef char ElemType;  
  9. typedef struct  
  10. {  
  11.     ElemType data[MaxSize];  
  12.     int front,rear;     /*队首和队尾指针*/  
  13. } SqQueue;  
  14.   
  15.   
  16.   
  17.   
  18. void InitQueue(SqQueue *&q);  //初始化顺序环形队列  
  19. void DestroyQueue(SqQueue *&q); //销毁顺序环形队列  
  20. bool QueueEmpty(SqQueue *q);  //判断顺序环形队列是否为空  
  21. int QueueLength(SqQueue *q);   //返回队列中元素个数,也称队列长度  
  22. bool enQueue(SqQueue *&q,ElemType e);   //进队  
  23. bool deQueue(SqQueue *&q,ElemType &e);  //出队  
  24.   
  25.   
  26. #endif // SQQUEUE_H_INCLUDED  
  • sqqueue.cpp代码
  1. //顺序队列基本运算函数  
  2. #include "sqqueue.h"  
  3.   
  4.   
  5. void InitQueue(SqQueue *&q)  //初始化顺序环形队列  
  6. {  
  7.     q=(SqQueue *)malloc (sizeof(SqQueue));  
  8.     q->front=q->rear=0;  
  9. }  
  10. void DestroyQueue(SqQueue *&q) //销毁顺序环形队列  
  11. {  
  12.     free(q);  
  13. }  
  14. bool QueueEmpty(SqQueue *q)  //判断顺序环形队列是否为空  
  15. {  
  16.     return(q->front==q->rear);  
  17. }  
  18.   
  19.   
  20.   
  21.   
  22. int QueueLength(SqQueue *q)   //返回队列中元素个数,也称队列长度  
  23. {  
  24.     return (q->rear-q->front+MaxSize)%MaxSize;  
  25. }  
  26.   
  27.   
  28. bool enQueue(SqQueue *&q,ElemType e)   //进队  
  29. {  
  30.     if ((q->rear+1)%MaxSize==q->front)  //队满上溢出  
  31.         return false;  
  32.     q->rear=(q->rear+1)%MaxSize;  
  33.     q->data[q->rear]=e;  
  34.     return true;  
  35. }  
  36. bool deQueue(SqQueue *&q,ElemType &e)  //出队  
  37. {  
  38.     if (q->front==q->rear)      //队空下溢出  
  39.         return false;  
  40.     q->front=(q->front+1)%MaxSize;  
  41.     e=q->data[q->front];  
  42.     return true;  
  43. }  
  • main.cpp文件代码
  1. #include "sqqueue.h"  
  2.   
  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. }  

运行结果:



识点总结:
定义顺序环形队列算法库要理解队首和队尾指针的用法以及环形结构这一特殊形式的实现。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值