第七周项目1

来源:互联网 发布:施工计划软件 编辑:程序博客网 时间:2024/05/29 19:38

下图是数据存储结构设计及各种操作实现的要点: 


顺序环形队列算法库采用程序的多文件组织形式,包括两个文件: 
   
  1.头文件:sqqueue.h,包含定义顺序环形队列数据结构的代码、宏定义、要实现算法的函数的声明:

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

2.源文件:sqqueue.cpp,包含实现各种算法的函数的定义

[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include "sqqueue.h"  
  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. int QueueLength(SqQueue *q)   //返回队列中元素个数,也称队列长度  
  21. {  
  22.     return (q->rear-q->front+MaxSize)%MaxSize;  
  23. }  
  24.   
  25. bool enQueue(SqQueue *&q,ElemType e)   //进队  
  26. {  
  27.     if ((q->rear+1)%MaxSize==q->front)  //队满上溢出  
  28.         return false;  
  29.     q->rear=(q->rear+1)%MaxSize;  
  30.     q->data[q->rear]=e;  
  31.     return true;  
  32. }  
  33. bool deQueue(SqQueue *&q,ElemType &e)  //出队  
  34. {  
  35.     if (q->front==q->rear)      //队空下溢出  
  36.         return false;  
  37.     q->front=(q->front+1)%MaxSize;  
  38.     e=q->data[q->front];  
  39.     return true;  
  40. }  


3.在同一项目(project)中建立一个源文件(如main.cpp),编制main函数,完成相关的测试工作。 例:
[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. }  

原创粉丝点击