第七周排队看病模拟(队列)
来源:互联网 发布:seo外链工具有用吗? 编辑:程序博客网 时间:2024/05/22 17:12
- #ifndef LIQUEUE_H_INCLUDED
- #define LIQUEUE_H_INCLUDED
- #include<malloc.h>
- typedef int ElemType;
- typedef struct qnode
- {
- ElemType data;
- struct qnode *next;
- } QNode;
-
- typedef struct
- {
- QNode *front;
- QNode *rear;
- } LiQueue;
- void InitQueue(LiQueue *&q);
- void DestroyQueue(LiQueue *&q);
- bool QueueEmpty(LiQueue *q);
- int QueueLength(LiQueue *q);
- void enQueue(LiQueue *&q,ElemType e);
- bool deQueue(LiQueue *&q,ElemType &e);
-
- #endif // LIQUEUE_H_INCLUDED
- #include <iostream>
- #include "head.h"
- #include<stdio.h>
- #define N 100
- int main()
- {
- LiQueue *qu;
- ElemType a,no,ne[N],i=0;
- InitQueue(qu);
- while(1)
- {
- printf("1.排队 2.就诊 3.查看排队 4.不再排队,余下依次就诊 5.下班");
- printf("\n");
- printf("请选择:");
- scanf("%d",&a);
-
- switch(a)
- {
- case 1:
- {
- printf(">>输入病历号:");
- scanf("%d",&no);
- enQueue(qu,no);
- }
- break;
- case 2:
- {
- printf(">>病人");
- deQueue(qu,no);
- printf("%d",no);
- printf("就诊");
- printf("\n");
- }
- break;
- case 3:
- {
- printf(">>排队病人:");
- while(!QueueEmpty(qu))
- {
- deQueue(qu,ne[i]);
- printf("%d ",ne[i]);
- i++;
- }
- printf("\n");
- }
- break;
- case 4:
- {
- printf(">>病人按以下顺序就诊:");
- for(int j=0; j<i; j++)
- {
- printf("%d ",ne[j]);
- }
- printf("\n");
- }
- break;
- default:
- a=5;
-
-
-
-
-
-
-
-
- printf("下班");
- printf("\n");
- return 0;
- }
- }
- }
- #include "head.h"
- void InitQueue(LiQueue *&q)
- {
- q=(LiQueue *)malloc(sizeof(LiQueue));
- q->front=q->rear=NULL;
- }
- void DestroyQueue(LiQueue *&q)
- {
- QNode *r,*p=q->front;
- if(p!=NULL)
- {
- r=p->next;
- while(r!=NULL)
- {
- free(p);
- p=r;
- r=p->next;
- }
- }
- free(p);
- free(q);
- }
- bool QueueEmpty(LiQueue *q)
- {
- return q->rear==NULL;
- }
- int QueueLength(LiQueue *q)
- {
- return q->rear->data;
- }
- void enQueue(LiQueue *&q,ElemType e)
- {
- QNode *p;
- p=(QNode *)malloc(sizeof(QNode));
- p->data=e;
- p->next=NULL;
- if(q->rear==NULL)
- q->rear=q->front=p;
- else
- {
- q->rear->next=p;
- q->rear=p;
- }
- }
- bool deQueue(LiQueue *&q,ElemType &e)
- {
- QNode *t;
- t=q->front;
- if(q->rear==NULL)
- return false;
- if(q->rear==q->front)
- q->rear=q->front=NULL;
- else
- {
- q->front=q->front->next;
- }
- e=t->data;
- free(t);
- return true;
- }
运行结果:
知识点总结:
与老师所给程序不同,我个人是利用链式队列算法库进行构造的,主函数有较大的改动,可能有一些不足的地方,还望指出,如果有谁可以进一步提升功能那就再好不过了。
- #ifndef LIQUEUE_H_INCLUDED
- #define LIQUEUE_H_INCLUDED
- #include<malloc.h>
- typedef int ElemType;
- typedef struct qnode
- {
- ElemType data;
- struct qnode *next;
- } QNode;
-
- typedef struct
- {
- QNode *front;
- QNode *rear;
- } LiQueue;
- void InitQueue(LiQueue *&q);
- void DestroyQueue(LiQueue *&q);
- bool QueueEmpty(LiQueue *q);
- int QueueLength(LiQueue *q);
- void enQueue(LiQueue *&q,ElemType e);
- bool deQueue(LiQueue *&q,ElemType &e);
-
- #endif // LIQUEUE_H_INCLUDED
- #include <iostream>
- #include "head.h"
- #include<stdio.h>
- #define N 100
- int main()
- {
- LiQueue *qu;
- ElemType a,no,ne[N],i=0;
- InitQueue(qu);
- while(1)
- {
- printf("1.排队 2.就诊 3.查看排队 4.不再排队,余下依次就诊 5.下班");
- printf("\n");
- printf("请选择:");
- scanf("%d",&a);
-
- switch(a)
- {
- case 1:
- {
- printf(">>输入病历号:");
- scanf("%d",&no);
- enQueue(qu,no);
- }
- break;
- case 2:
- {
- printf(">>病人");
- deQueue(qu,no);
- printf("%d",no);
- printf("就诊");
- printf("\n");
- }
- break;
- case 3:
- {
- printf(">>排队病人:");
- while(!QueueEmpty(qu))
- {
- deQueue(qu,ne[i]);
- printf("%d ",ne[i]);
- i++;
- }
- printf("\n");
- }
- break;
- case 4:
- {
- printf(">>病人按以下顺序就诊:");
- for(int j=0; j<i; j++)
- {
- printf("%d ",ne[j]);
- }
- printf("\n");
- }
- break;
- default:
- a=5;
-
-
-
-
-
-
-
-
- printf("下班");
- printf("\n");
- return 0;
- }
- }
- }
- #include "head.h"
- void InitQueue(LiQueue *&q)
- {
- q=(LiQueue *)malloc(sizeof(LiQueue));
- q->front=q->rear=NULL;
- }
- void DestroyQueue(LiQueue *&q)
- {
- QNode *r,*p=q->front;
- if(p!=NULL)
- {
- r=p->next;
- while(r!=NULL)
- {
- free(p);
- p=r;
- r=p->next;
- }
- }
- free(p);
- free(q);
- }
- bool QueueEmpty(LiQueue *q)
- {
- return q->rear==NULL;
- }
- int QueueLength(LiQueue *q)
- {
- return q->rear->data;
- }
- void enQueue(LiQueue *&q,ElemType e)
- {
- QNode *p;
- p=(QNode *)malloc(sizeof(QNode));
- p->data=e;
- p->next=NULL;
- if(q->rear==NULL)
- q->rear=q->front=p;
- else
- {
- q->rear->next=p;
- q->rear=p;
- }
- }
- bool deQueue(LiQueue *&q,ElemType &e)
- {
- QNode *t;
- t=q->front;
- if(q->rear==NULL)
- return false;
- if(q->rear==q->front)
- q->rear=q->front=NULL;
- else
- {
- q->front=q->front->next;
- }
- e=t->data;
- free(t);
- return true;
- }
运行结果:
知识点总结:
与老师所给程序不同,我个人是利用链式队列算法库进行构造的,主函数有较大的改动,可能有一些不足的地方,还望指出,如果有谁可以进一步提升功能那就再好不过了。
0 0