第七周--项目3负数把正数赶出队列

来源:互联网 发布:重庆爱知日语招聘 编辑:程序博客网 时间:2024/06/03 23:05
  1. *Copyright(c)2016,烟台大学计算机与控制工程学院  
  2.  *All right reserved.  
  3.  *文件名称:负数把正数赶出队列.cpp  
  4.  *作者:陈晓琳  
  5.  *完成日期;2016年10月14日  
  6.  *版本号;v1.0  
  7.  *  
  8.  *问题描述: 设从键盘输入一整数序列a1,a2,…an,试编程实现:  
  9.  当ai>0时,ai进队,当ai<0时,将队首元素出队,当ai=0时,表示输入结束。  
  10.  要求将队列处理成环形队列,使用算法库中定义的数据类型及算法,程序中只包括一个函数(main函数),  
  11.  入队和出队等操作直接写在main函数中即可。  
  12.  当进队出队异常(如队满)时,要打印出错信息。  
  13.   
  14.   
  15.   
  16.  *输入描述:根据输入要求输入任何正数或者负数  
  17.  *程序输出:队列是否未满是否为空或者入队和出队  
  18. */  
  19.   
  20.   
  21. #include <stdio.h>  
  22. #include <malloc.h>  
  23. #include "sqqueue.h"  
  24.   
  25. int main()  
  26. {  
  27.     ElemType a,x;  
  28.     SqQueue *qu;    //定义队列  
  29.     InitQueue(qu);  //队列初始化  
  30.     while (1)  
  31.     {  
  32.         printf("输入a值(输入正数进队,负数出队,0结束):");  
  33.         scanf("%d", &a);  
  34.         if (a>0)  
  35.         {  
  36.             if (!enQueue(qu,a))  
  37.                 printf("  队列满,不能入队\n");  
  38.         }  
  39.         else if (a<0)  
  40.         {  
  41.             if (!deQueue(qu, x))  
  42.                 printf("  队列空,不能出队\n");  
  43.         }  
  44.         else  
  45.             break;  
  46.     }  
  47.     return 0;  
  48. }  
  49. #include <stdio.h>  
  50. #include <malloc.h>  
  51. #include "sqqueue.h"  
  52.   
  53. void InitQueue(SqQueue *&q)  //初始化顺序环形队列  
  54. {  
  55.     q=(SqQueue *)malloc (sizeof(SqQueue));  
  56.     q->front=q->rear=0;  
  57. }  
  58. void DestroyQueue(SqQueue *&q) //销毁顺序环形队列  
  59. {  
  60.     free(q);  
  61. }  
  62. bool QueueEmpty(SqQueue *q)  //判断顺序环形队列是否为空  
  63. {  
  64.     return(q->front==q->rear);  
  65. }  
  66.   
  67.   
  68. int QueueLength(SqQueue *q)   //返回队列中元素个数,也称队列长度  
  69. {  
  70.     return (q->rear-q->front+MaxSize)%MaxSize;  
  71. }  
  72.   
  73. bool enQueue(SqQueue *&q,ElemType e)   //进队  
  74. {  
  75.     if ((q->rear+1)%MaxSize==q->front)  //队满上溢出  
  76.         return false;  
  77.     q->rear=(q->rear+1)%MaxSize;  
  78.     q->data[q->rear]=e;  
  79.     return true;  
  80. }  
  81. bool deQueue(SqQueue *&q,ElemType &e)  //出队  
  82. {  
  83.     if (q->front==q->rear)      //队空下溢出  
  84.         return false;  
  85.     q->front=(q->front+1)%MaxSize;  
  86.     e=q->data[q->front];  
  87.     return true;  
  88. }  
  89. #ifndef SQQUEUE_H_INCLUDED  
  90. #define SQQUEUE_H_INCLUDED  
  91.   
  92.   
  93. #define MaxSize 5  
  94. typedef int ElemType;  
  95. typedef struct  
  96. {  
  97.     ElemType data[MaxSize];  
  98.     int front,rear;     /*队首和队尾指针*/  
  99. } SqQueue;  
  100.   
  101.   
  102. void InitQueue(SqQueue *&q);  //初始化顺序环形队列  
  103. void DestroyQueue(SqQueue *&q); //销毁顺序环形队列  
  104. bool QueueEmpty(SqQueue *q);  //判断顺序环形队列是否为空  
  105. int QueueLength(SqQueue *q);   //返回队列中元素个数,也称队列长度  
  106. bool enQueue(SqQueue *&q,ElemType e);   //进队  
  107. bool deQueue(SqQueue *&q,ElemType &e);  //出队  
  108.   
  109.   
  110. #endif // SQQUEUE_H_INCLUDED  

运行结果:


知识点总结:

利用环形顺序队列进行编写,头函数利用入队和出队的函数为布尔函数的特点,如果队满则不能入队,相反队空则不能出队

学习心得:

要灵活运用所学知识

0 0