用队列模拟病人排队.docx
《用队列模拟病人排队.docx》由会员分享,可在线阅读,更多相关《用队列模拟病人排队.docx(10页珍藏版)》请在冰豆网上搜索。
用队列模拟病人排队
数据结构课程设计报告
(2015/2016学年第2学期)
学生:
学生专业:
计算机科学与技术
学生班级:
学生学号:
上机环境:
VisualC++6.0
2016年4月23日
实验题目:
编写一个程序exp3—7.cpp,反应病人到医院看病,排队看医生的情况。
要求模拟病人等待就诊这一过程。
程序采用菜单方式,其选项及功能说明如下:
排队——输入排队病人的病历号,加入到病人排队队列中;
就诊——病人排队队列中最前面的病人就诊,并将其从队列中删除;
查看排队——从队首到队尾列出所有的排队病人的病历号;
不再排队,余下依次就诊——从队首到队尾列出所有的排队病人的病历号,并退出运行;
下班——退出运行。
实验文件:
exp3_7.h
#ifndefexp3_7_h
#defineexp3_7_h
#define_Nmax11
#include
usingnamespacestd;
typedefcharType;
typedefstructpatient
{
Typename[20];
intage;
Typesex[5];
Typenumber[_Nmax];
}ElemType;
typedefstructqnode
{
ElemTypedata;
structqnode*next;
}QNode;
typedefstruct
{
QNode*front;
QNode*rear;
}LiQueue;
voidInitQueue(LiQueue*&q);//初始化队列
voidDestroyQueue(LiQueue*&q);//销毁队列
boolQueueEmpty(LiQueue*&q);//判断队列是否为空
voidenQueue(LiQueue*&q,ElemTypee);//进队列
booldeQueue(LiQueue*&q,ElemType&e);//出队列
voidscanf_pat(ElemType&p);//输入病人的信息
voidprintf_pat(ElemType&e);//输出下一个病人的信息
voidoperation();//操作
voidprintf_Menu();//菜单
#endif
exp3_7.cpp
#include
#include"exp3_7.h"
#include
usingnamespacestd;
voidInitQueue(LiQueue*&q)
{
q=(LiQueue*)malloc(sizeof(LiQueue));
q->front=q->rear=NULL;
}
voidDestroyQueue(LiQueue*&q)
{
QNode*p=q->front,*r;
if(p!
=NULL)
{
r=p->next;
while(r!
=NULL)
{
free(p);
p=r;
r=p->next;
}
}
free(p);
free(q);
}
boolQueueEmpty(LiQueue*&q)
{
return(q->rear==NULL);
}
voidenQueue(LiQueue*&q,ElemTypee)
{
QNode*p;
p=(QNode*)malloc(sizeof(QNode));
p->data=e;
p->next=NULL;
if(q->rear==NULL)
q->front=q->rear=p;
else
{
q->rear->next=p;
q->rear=p;
}
}
booldeQueue(LiQueue*&q,ElemType&e)
{
QNode*t;
if(q->rear==NULL)
returnfalse;
t=q->front;
if(q->front==q->rear)
q->front=q->rear=NULL;
else
q->front=q->front->next;
e=t->data;
free(t);
returntrue;
}
voidscanf_pat(ElemType&p)
{
cout<<"输入病人的:
";
cin>>p.name;
p.name[19]='\0';
cout<<"输入病人的性别:
";
cin>>p.sex;
p.sex[4]='\0';
cout<<"输入病人的年龄:
";
cin>>p.age;
cout<<"输入病人的编号:
";
cin>>p.number;
p.number[_Nmax-1]='\0';
}
voidprintf_pat(ElemType&e)
{
cout<<"下一个病人"<<'\n'
<<"姓名:
"<<<"性别:
"<<<"年龄:
"<<<"病历号:
"<}
voidoperation()
{
ElemTypep,e;
LiQueue*q;
intn;
InitQueue(q);
printf_Menu();
cin>>n;
cout<while(n!
=5)
{
switch(n)
{
case1:
scanf_pat(p);
enQueue(q,p);break;
case2:
if(deQueue(q,e)==true)
printf_pat(e);break;
case3:
QNode*t;
t=q->front;
while(q->front!
=NULL)
{
e=q->front->data;
printf_pat(e);
q->front=q->front->next;
};
q->front=t;break;
case4:
while(q->front!
=NULL)
{
if(deQueue(q,e)==true)
printf_pat(e);
};break;
}
cout<<"请选择你所需的功能:
";
cin>>n;
cout<}
}
voidprintf_Menu()
{
cout<<"******************chooseyourfunction*********************"<cout<<"1.排队2.就诊"<cout<<"3.查看排队4.不再排队,余下依次就诊"<cout<<"5.退出运行"<cout<<"请选择你所需要的功能:
";
}
exp3_7_main.cpp
#include
#include
usingnamespacestd;
#include"exp3_7.h"
intmain()
{
operation();
return0;
}
算法描述:
程序采用链队储存数据,用switch函数进行功能选择:
1.排队操作采用函数enQueue(q,e)进队;2.就诊操作使用deQueue(q,e)函数和printf(e)函数实现;3.查看排队采用q->front依次输出数据;4.不再排队,余下病人就诊用while语句和deQueue(q,e)函数和printf(e)函数实现;5.退出
实验数据: