七周 1 建立顺序环形队列算法库

/*
Copyright (c)2016,烟台大学计算机与控制工程学院
All rights reserved.
文件名称:项目1.cbp
作 者:泮春宇
完成日期:2016年10月21日
版 本 号:v1.0


问题描述:定义顺序环形队列存储结构,实现其基本运算,并完成测试。
输入描述:无
程序输出:测试数据
*/

  • sqqueue.h头文件代码

 

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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_INCLUDEDsqqueue.h是顺序队列的一个算法库集合,里面声明了常用到的各个功能函数。  
  27. <span style="font-size:24px;"><strong>•sqqueue.cpp文件代码</strong>  
  28. </span>//顺序队列基本运算函数  
  29. #include "sqqueue.h"  
  30.   
  31.   
  32. void InitQueue(SqQueue *&q)  //初始化顺序环形队列  
  33. {  
  34.     q=(SqQueue *)malloc (sizeof(SqQueue));  
  35.     q->front=q->rear=0;  
  36. }  
  37. void DestroyQueue(SqQueue *&q) //销毁顺序环形队列  
  38. {  
  39.     free(q);  
  40. }  
  41. bool QueueEmpty(SqQueue *q)  //判断顺序环形队列是否为空  
  42. {  
  43.     return(q->front==q->rear);  
  44. }  
  45.   
  46.   
  47.   
  48.   
  49. int QueueLength(SqQueue *q)   //返回队列中元素个数,也称队列长度  
  50. {  
  51.     return (q->rear-q->front+MaxSize)%MaxSize;  
  52. }  
  53.   
  54.   
  55. bool enQueue(SqQueue *&q,ElemType e)   //进队  
  56. {  
  57.     if ((q->rear+1)%MaxSize==q->front)  //队满上溢出  
  58.         return false;  
  59.     q->rear=(q->rear+1)%MaxSize;  
  60.     q->data[q->rear]=e;  
  61.     return true;  
  62. }  
  63. bool deQueue(SqQueue *&q,ElemType &e)  //出队  
  64. {  
  65.     if (q->front==q->rear)      //队空下溢出  
  66.         return false;  
  67.     q->front=(q->front+1)%MaxSize;  
  68.     e=q->data[q->front];  
  69.     return true;  
  70. }  
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. •<strong><span style="font-size:24px;">main.cpp文件代码  
  2. </span></strong>#include "sqqueue.h"  
  3.   
  4.   
  5. int main()  
  6. {  
  7.     ElemType e;  
  8.     SqQueue *q;  
  9.     printf("(1)初始化队列q\n");  
  10.     InitQueue(q);  
  11.     printf("(2)依次进队列元素a,b,c\n");  
  12.     if (enQueue(q,'a')==0) printf("队满,不能进队\n");  
  13.     if (enQueue(q,'b')==0) printf("队满,不能进队\n");  
  14.     if (enQueue(q,'c')==0) printf("队满,不能进队\n");  
  15.     printf("(3)队列为%s\n",(QueueEmpty(q)?"空":"非空"));  
  16.     if (deQueue(q,e)==0)  
  17.         printf("队空,不能出队\n");  
  18.     else  
  19.         printf("(4)出队一个元素%c\n",e);  
  20.     printf("(5)队列q的元素个数:%d\n",QueueLength(q));  
  21.     printf("(6)依次进队列元素d,e,f\n");  
  22.     if (enQueue(q,'d')==0) printf("队满,不能进队\n");  
  23.     if (enQueue(q,'e')==0) printf("队满,不能进队\n");  
  24.     if (enQueue(q,'f')==0) printf("队满,不能进队\n");  
  25.     printf("(7)队列q的元素个数:%d\n",QueueLength(q));  
  26.     printf("(8)出队列序列:");  
  27.     while (!QueueEmpty(q))  
  28.     {  
  29.         deQueue(q,e);  
  30.         printf("%c ",e);  
  31.     }  
  32.     printf("\n");  
  33.     printf("(9)释放队列\n");  
  34.     DestroyQueue(q);  
  35.     return 0;  
  36. }  


运行结果:
知识点总结:
定义顺序环形队列算法库要理解队首和队尾指针的用法。

学习心得:

初始化时队首和队尾指针要赋值为零,这一点和栈有区别。

判断顺序环形队列是否为空只需要看队首和队尾是否相等就可以了。

进队出队都要用到运算符‘%’,以便环形结构的实现。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值