第七周项目一 顺序环形队列

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