第七周排队看病模拟(队列)

来源:互联网 发布:seo外链工具有用吗? 编辑:程序博客网 时间:2024/05/22 17:12
  1. /* 
  2.  *Copyright (c) 2016,烟台大学计算机学院 
  3.  *All rights reserved. 
  4.  *文件名称:houzhuibiaodashi.cpp 
  5.  *作者:衣龙川 
  6.  *完成日期:2016年10月20日 
  7.  *版本号:vc++6.0 
  8.  * 
  9.  *问题描述:排队看病模拟
  10.  *输入描述:编号和排队号 
  11.  *程序输出:排队看病次序,下一个排队看病人的排队号 
  12. */  



[cpp] view plain copy
  1. #ifndef LIQUEUE_H_INCLUDED  
  2. #define LIQUEUE_H_INCLUDED  
  3. #include<malloc.h>  
  4. typedef int ElemType;  
  5. typedef struct qnode  
  6. {  
  7.     ElemType data;  
  8.     struct qnode *next;  
  9. } QNode;        //链队数据结点类型定义  
  10.   
  11. typedef struct  
  12. {  
  13.     QNode *front;  
  14.     QNode *rear;  
  15. } LiQueue;          //链队类型定义  
  16. void InitQueue(LiQueue *&q);  //初始化链队  
  17. void DestroyQueue(LiQueue *&q);  //销毁链队  
  18. bool QueueEmpty(LiQueue *q);  //判断链队是否为空  
  19. int QueueLength(LiQueue *q);  //返回队列中数据元素个数  
  20. void enQueue(LiQueue *&q,ElemType e);  //入队  
  21. bool deQueue(LiQueue *&q,ElemType &e);   //出队  
  22.   
  23. #endif // LIQUEUE_H_INCLUDED  


[cpp] view plain copy
  1. #include <iostream>  
  2. #include "head.h"  
  3. #include<stdio.h>  
  4. #define N 100  
  5. int main()  
  6. {  
  7.     LiQueue *qu;  
  8.     ElemType a,no,ne[N],i=0;  
  9.     InitQueue(qu);  
  10.     while(1)  
  11.     {  
  12.         printf("1.排队 2.就诊 3.查看排队 4.不再排队,余下依次就诊 5.下班");  
  13.         printf("\n");  
  14.         printf("请选择:");  
  15.         scanf("%d",&a);  
  16.   
  17.         switch(a)  
  18.         {  
  19.         case 1:  
  20.         {  
  21.             printf(">>输入病历号:");  
  22.             scanf("%d",&no);  
  23.             enQueue(qu,no);  
  24.         }  
  25.         break;  
  26.         case 2:  
  27.         {  
  28.             printf(">>病人");  
  29.             deQueue(qu,no);  
  30.             printf("%d",no);  
  31.             printf("就诊");  
  32.             printf("\n");  
  33.         }  
  34.         break;  
  35.         case 3:  
  36.         {  
  37.             printf(">>排队病人:");  
  38.             while(!QueueEmpty(qu))  
  39.             {  
  40.                 deQueue(qu,ne[i]);  
  41.                 printf("%d ",ne[i]);  
  42.                 i++;  
  43.             }  
  44.             printf("\n");  
  45.         }  
  46.         break;  
  47.         case 4:  
  48.         {  
  49.             printf(">>病人按以下顺序就诊:");  
  50.             for(int j=0; j<i; j++)  
  51.             {  
  52.                 printf("%d ",ne[j]);  
  53.             }  
  54.            printf("\n");  
  55.         }  
  56.         break;  
  57.         default:  
  58.             a=5;  
  59.   
  60.             /*case 5: 
  61.             { 
  62.                 printf("下班"); 
  63.                 printf("\n"); 
  64.             } 
  65.             break; 
  66.             }*/  
  67.             printf("下班");  
  68.             printf("\n");  
  69.             return 0;  
  70.         }  
  71.     }  
  72. }  


[cpp] view plain copy
  1. #include "head.h"  
  2. void InitQueue(LiQueue *&q)  
  3. {  
  4.     q=(LiQueue *)malloc(sizeof(LiQueue));  
  5.     q->front=q->rear=NULL;  
  6. }  
  7. void DestroyQueue(LiQueue *&q)  
  8. {  
  9.     QNode *r,*p=q->front;  
  10.     if(p!=NULL)  
  11.     {  
  12.         r=p->next;  
  13.         while(r!=NULL)  
  14.         {  
  15.             free(p);  
  16.             p=r;  
  17.             r=p->next;  
  18.         }  
  19.     }  
  20.     free(p);  
  21.     free(q);  
  22. }  
  23. bool QueueEmpty(LiQueue *q)  
  24. {  
  25.     return q->rear==NULL;  
  26. }  
  27. int QueueLength(LiQueue *q)  
  28. {  
  29.     return q->rear->data;  
  30. }  
  31. void enQueue(LiQueue *&q,ElemType e)  
  32. {  
  33.     QNode *p;  
  34.     p=(QNode *)malloc(sizeof(QNode));  
  35.     p->data=e;  
  36.     p->next=NULL;  
  37.     if(q->rear==NULL)  
  38.         q->rear=q->front=p;  
  39.     else  
  40.     {  
  41.         q->rear->next=p;  
  42.         q->rear=p;  
  43.     }  
  44. }  
  45. bool deQueue(LiQueue *&q,ElemType &e)  
  46. {  
  47.     QNode *t;  
  48.     t=q->front;  
  49.     if(q->rear==NULL)  
  50.         return false;  
  51.     if(q->rear==q->front)  
  52.         q->rear=q->front=NULL;  
  53.         else  
  54.         {  
  55.             q->front=q->front->next;  
  56.         }  
  57.     e=t->data;  
  58.     free(t);  
  59.     return true;  
  60. }  

运行结果:


知识点总结:

与老师所给程序不同,我个人是利用链式队列算法库进行构造的,主函数有较大的改动,可能有一些不足的地方,还望指出,如果有谁可以进一步提升功能那就再好不过了。



[cpp] view plain copy
  1. #ifndef LIQUEUE_H_INCLUDED  
  2. #define LIQUEUE_H_INCLUDED  
  3. #include<malloc.h>  
  4. typedef int ElemType;  
  5. typedef struct qnode  
  6. {  
  7.     ElemType data;  
  8.     struct qnode *next;  
  9. } QNode;        //链队数据结点类型定义  
  10.   
  11. typedef struct  
  12. {  
  13.     QNode *front;  
  14.     QNode *rear;  
  15. } LiQueue;          //链队类型定义  
  16. void InitQueue(LiQueue *&q);  //初始化链队  
  17. void DestroyQueue(LiQueue *&q);  //销毁链队  
  18. bool QueueEmpty(LiQueue *q);  //判断链队是否为空  
  19. int QueueLength(LiQueue *q);  //返回队列中数据元素个数  
  20. void enQueue(LiQueue *&q,ElemType e);  //入队  
  21. bool deQueue(LiQueue *&q,ElemType &e);   //出队  
  22.   
  23. #endif // LIQUEUE_H_INCLUDED  


[cpp] view plain copy
  1. #include <iostream>  
  2. #include "head.h"  
  3. #include<stdio.h>  
  4. #define N 100  
  5. int main()  
  6. {  
  7.     LiQueue *qu;  
  8.     ElemType a,no,ne[N],i=0;  
  9.     InitQueue(qu);  
  10.     while(1)  
  11.     {  
  12.         printf("1.排队 2.就诊 3.查看排队 4.不再排队,余下依次就诊 5.下班");  
  13.         printf("\n");  
  14.         printf("请选择:");  
  15.         scanf("%d",&a);  
  16.   
  17.         switch(a)  
  18.         {  
  19.         case 1:  
  20.         {  
  21.             printf(">>输入病历号:");  
  22.             scanf("%d",&no);  
  23.             enQueue(qu,no);  
  24.         }  
  25.         break;  
  26.         case 2:  
  27.         {  
  28.             printf(">>病人");  
  29.             deQueue(qu,no);  
  30.             printf("%d",no);  
  31.             printf("就诊");  
  32.             printf("\n");  
  33.         }  
  34.         break;  
  35.         case 3:  
  36.         {  
  37.             printf(">>排队病人:");  
  38.             while(!QueueEmpty(qu))  
  39.             {  
  40.                 deQueue(qu,ne[i]);  
  41.                 printf("%d ",ne[i]);  
  42.                 i++;  
  43.             }  
  44.             printf("\n");  
  45.         }  
  46.         break;  
  47.         case 4:  
  48.         {  
  49.             printf(">>病人按以下顺序就诊:");  
  50.             for(int j=0; j<i; j++)  
  51.             {  
  52.                 printf("%d ",ne[j]);  
  53.             }  
  54.            printf("\n");  
  55.         }  
  56.         break;  
  57.         default:  
  58.             a=5;  
  59.   
  60.             /*case 5: 
  61.             { 
  62.                 printf("下班"); 
  63.                 printf("\n"); 
  64.             } 
  65.             break; 
  66.             }*/  
  67.             printf("下班");  
  68.             printf("\n");  
  69.             return 0;  
  70.         }  
  71.     }  
  72. }  


[cpp] view plain copy
  1. #include "head.h"  
  2. void InitQueue(LiQueue *&q)  
  3. {  
  4.     q=(LiQueue *)malloc(sizeof(LiQueue));  
  5.     q->front=q->rear=NULL;  
  6. }  
  7. void DestroyQueue(LiQueue *&q)  
  8. {  
  9.     QNode *r,*p=q->front;  
  10.     if(p!=NULL)  
  11.     {  
  12.         r=p->next;  
  13.         while(r!=NULL)  
  14.         {  
  15.             free(p);  
  16.             p=r;  
  17.             r=p->next;  
  18.         }  
  19.     }  
  20.     free(p);  
  21.     free(q);  
  22. }  
  23. bool QueueEmpty(LiQueue *q)  
  24. {  
  25.     return q->rear==NULL;  
  26. }  
  27. int QueueLength(LiQueue *q)  
  28. {  
  29.     return q->rear->data;  
  30. }  
  31. void enQueue(LiQueue *&q,ElemType e)  
  32. {  
  33.     QNode *p;  
  34.     p=(QNode *)malloc(sizeof(QNode));  
  35.     p->data=e;  
  36.     p->next=NULL;  
  37.     if(q->rear==NULL)  
  38.         q->rear=q->front=p;  
  39.     else  
  40.     {  
  41.         q->rear->next=p;  
  42.         q->rear=p;  
  43.     }  
  44. }  
  45. bool deQueue(LiQueue *&q,ElemType &e)  
  46. {  
  47.     QNode *t;  
  48.     t=q->front;  
  49.     if(q->rear==NULL)  
  50.         return false;  
  51.     if(q->rear==q->front)  
  52.         q->rear=q->front=NULL;  
  53.         else  
  54.         {  
  55.             q->front=q->front->next;  
  56.         }  
  57.     e=t->data;  
  58.     free(t);  
  59.     return true;  
  60. }  

运行结果:

知识点总结:

  与老师所给程序不同,我个人是利用链式队列算法库进行构造的,主函数有较大的改动,可能有一些不足的地方,还望指出,如果有谁可以进一步提升功能那就再好不过了。


0 0
原创粉丝点击