第七周项目一

来源:互联网 发布:上海培训公司java 编辑:程序博客网 时间:2024/05/22 00:49
  1. /*  
  2. 烟台大学计算机学院  
  3.   
  4. 文件名称:xiangmu.cpp  
  5.   
  6. 作者:刘照京  
  7.   
  8. 完成日期:2017年10月26日  
  9.   
  10. 问题描述:顺序环形队列 
  11.   
  12. 输入描述:无 
  13.   
  14. 输出描述:队列元素,出列入列元素 
  15.   
  16. */   
  17.   
  18. sqqueue.h:  
  19.   
  20. #include <stdio.h>  
  21. #define MaxSize 100  
  22. typedef int ElemType;  
  23.   
  24. typedef struct  
  25. {  
  26.     ElemType data[MaxSize];  
  27.   
  28.     int front;  
  29.   
  30.     int rear;  
  31. }SqQueue;  
  32. void InitQueue(SqQueue *&q);  //初始化顺序环形队列  
  33. void DestroyQueue(SqQueue *&q); //销毁顺序环形队列  
  34. bool QueueEmpty(SqQueue *q);  //判断顺序环形队列是否为空  
  35. int QueueLength(SqQueue *q);   //返回队列中元素个数,也称队列长度  
  36. bool enQueue(SqQueue *&q,ElemType e);   //进队  
  37. bool deQueue(SqQueue *&q,ElemType &e);  //出队  
  38.   
  39.   
  40. sqqueue.cpp:  
  41.   
  42. #include "sqqueue.h"  
  43. #include <stdio.h>  
  44. #include <malloc.h>  
  45.   
  46. void InitQueue(SqQueue *&q)  
  47. {  
  48.     q=(SqQueue*)malloc(sizeof(SqQueue));//动态开辟内存  
  49.   
  50.     q->front=q->rear=-1;  
  51. }  
  52.   
  53.   
  54. void DestroyQueue(SqQueue *&q)  
  55. {  
  56.     free(q);  
  57.   
  58. }  
  59.   
  60. bool QueueEmpty(SqQueue *q)  
  61. {  
  62.     return(q->front==q->rear);  
  63. }  
  64.   
  65. int QueueLength(SqQueue *q)  
  66. {  
  67.     return(q->rear);//返回rear即为长度  
  68. }  
  69.   
  70.   
  71. bool enQueue(SqQueue *&q,ElemType e)  
  72. {  
  73.     if((q->rear+1)%MaxSize==q->front)//队满  
  74.     {  
  75.         return false;  
  76.   
  77.     }  
  78.   
  79.     q->rear=(q->rear+1)%MaxSize;  
  80.   
  81.     q->data[q->rear]=e;  
  82.   
  83.     return true;  
  84. }  
  85. bool deQueue(SqQueue *&q,ElemType &e)  
  86. {  
  87.    if(q->front==q->rear)//队空  
  88.   
  89.     return false;  
  90.   
  91.    q->front=(q->front+1)%MaxSize;  
  92.   
  93.    e=q->data[q->front];  
  94.   
  95.    return true;  
  96.   
  97.   
  98.   
  99.   
  100.   
  101.   
  102.   
  103. }  
  104.   
  105.   
  106.   
  107. main:  
  108.   
  109. #include "sqqueue.h"  
  110. #include <stdio.h>  
  111.   
  112.   
  113.   
  114. int main()  
  115. {  
  116.     ElemType e;  
  117.     SqQueue *q;  
  118.     printf("(1)初始化队列q\n");  
  119.     InitQueue(q);  
  120.     printf("(2)依次进队列元素a,b,c\n");  
  121.     if (enQueue(q,'a')==0) printf("队满,不能进队\n");  
  122.     if (enQueue(q,'b')==0) printf("队满,不能进队\n");  
  123.     if (enQueue(q,'c')==0) printf("队满,不能进队\n");  
  124.     printf("(3)队列为%s\n",(QueueEmpty(q)?"空":"非空"));  
  125.     if (deQueue(q,e)==0)  
  126.         printf("队空,不能出队\n");  
  127.     else  
  128.         printf("(4)出队一个元素%c\n",e);  
  129.     printf("(5)队列q的元素个数:%d\n",QueueLength(q));  
  130.     printf("(6)依次进队列元素d,e,f\n");  
  131.     if (enQueue(q,'d')==0) printf("队满,不能进队\n");  
  132.     if (enQueue(q,'e')==0) printf("队满,不能进队\n");  
  133.     if (enQueue(q,'f')==0) printf("队满,不能进队\n");  
  134.     printf("(7)队列q的元素个数:%d\n",QueueLength(q));  
  135.     printf("(8)出队列序列:");  
  136.     while (!QueueEmpty(q))  
  137.     {  
  138.         deQueue(q,e);  
  139.         printf("%c ",e);  
  140.     }  
  141.     printf("\n");  
  142.     printf("(9)释放队列\n");  
  143.     DestroyQueue(q);  
  144.     return 0;  
  145. }  


运行结果


学习心得:

学会了顺序环形队列的运算以及操作,判断是否为空以及销毁队列。

原创粉丝点击