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

来源:互联网 发布:苹果5怎么用4g网络 编辑:程序博客网 时间:2024/05/17 08:29
  1. /* 
  2.  
  3. copyright (t) 2016,烟台大学计算机学院 
  4.  
  5. *All rights reserved. 
  6.  
  7. *文件名称:1.cpp 
  8.  
  9. *作者:车金阳 
  10.  
  11. *完成日期:2016年11月8日 
  12.  
  13. *版本号:v1.0 
  14.  
  15. *问题描述:设从键盘输入一整数序列a1,a2,…an,试编程实现:当ai>0时,ai进队,当ai<0时,将队首元素出队,当ai=0时,表示输入结束。要求将队列处理成环形队列,使用算法库中定义的数据类型及算法,程序中只包括一个函数(main函数),入队和出队等操作直接写在main函数中即可。当进队出队异常(如队满)时,要打印出错信息。 
  16.  
  17. *输入描述:ai,ai=0时结束输入 
  18.  
  19. *程序输出:元素进队出队情况 
  20.  
  21. */  

sqqueue.h:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #define MaxSize 100  
  4. typedef char ElemType;  
  5. typedef struct  
  6. {  
  7.     ElemType data[MaxSize];  
  8.     int front,rear;  
  9. } SqQueue;  
  10. void InitQueue(SqQueue *&q);               //初始化顺序环形队列  
  11. void DestroyQueue(SqQueue *&q);            //销毁顺序环形队列  
  12. bool QueueEmpty(SqQueue *q);               //判断顺序环形队列是否为空  
  13. int QueueLength(SqQueue *q);               //返回队列中元素个数,也称队列长度  
  14. bool enQueue(SqQueue *&q,ElemType e);      //进队  
  15. bool deQueue(SqQueue *&q,ElemType &e);     //出队  

sqqueue.cpp:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include "sqqueue.h"  
  2. void InitQueue(SqQueue *&q)  //初始化顺序环形队列  
  3. {  
  4.     q=(SqQueue *)malloc(sizeof(SqQueue));  
  5.     q->front=q->rear=0;  
  6. }  
  7. void DestroyQueue(SqQueue *&q) //销毁顺序环形队列  
  8. {  
  9.     free(q);  
  10. }  
  11. bool QueueEmpty(SqQueue *q)  //判断顺序环形队列是否为空  
  12. {  
  13.     return (q->front==q->rear);  
  14. }  
  15. int QueueLength(SqQueue *q)   //返回队列中元素个数,也称队列长度  
  16. {  
  17.     return ((q->rear-q->front+MaxSize)%MaxSize);  
  18. }  
  19. bool enQueue(SqQueue *&q,ElemType e)   //进队  
  20. {  
  21.     if((q->rear+1)%MaxSize==q->front)  
  22.         return false;  
  23.     q->rear=(q->rear+1)%MaxSize;  
  24.     q->data[q->rear]=e;  
  25.     return true;  
  26. }  
  27. bool deQueue(SqQueue *&q,ElemType &e)  //出队  
  28. {  
  29.     if(q->front==q->rear)  
  30.         return false;  
  31.     q->front=(q->front+1)%MaxSize;  
  32.     e=q->data[q->front];  
  33.     return true;  
  34. }  

main.cpp:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #include "sqqueue.h"  
  3. int main()  
  4. {  
  5.     SqQueue *q;  
  6.     ElemType e;  
  7.     int ai;                                         //类型置为整型,便于调用scanf完成输入  
  8.   
  9.     while(1)                                        //while(1)设置多组输入  
  10.     {  
  11.         InitQueue(q);                               //初始化队列  
  12.         while(scanf("%d",&ai)!=EOF)                 //ai不为0,进入循环  
  13.         {  
  14.             if(ai==0)  
  15.                 break;  
  16.             else if(ai>0)  
  17.             {  
  18.                 if(enQueue(q,ai)==0)                //ai>0且队不满,ai进队  
  19.                     printf("队已满,进队失败!\n");  
  20.                 else  
  21.                     printf("元素%d已进队\n",ai);  
  22.             }  
  23.             else  
  24.             {  
  25.                 if(deQueue(q,e)==0)                 //ai<0且队不为空,队首元素出队  
  26.                     printf("队已空,出队失败!\n");  
  27.                 else  
  28.                     printf("队首元素%d已出队\n",e);  
  29.             }  
  30.         }  
  31.         printf("\n");  
  32.     }  
  33.     return 0;  
  34. }  

运行结果:

0 0