数据结构之看病排队系统
#include <iostream>
#include<stdio.h>
#include <malloc.h>
#define MaxSize 100
using namespace std;
typedef int ElemType;
typedef struct
{
ElemType data[MaxSize];
int front,rear;
} SqQueue;
void InitQueue(SqQueue *&q)
{ q=(SqQueue *)malloc (sizeof(SqQueue));
q->front=q->rear=0;
}
void DestroyQueue(SqQueue *&q)
{
free(q);
}
bool QueueEmpty(SqQueue *q)
{
return(q->front==q->rear);
}
bool enQueue(SqQueue *&q,ElemType e)
{ if ((q->rear+1)%MaxSize==q->front)
return false;
q->rear=(q->rear+1)%MaxSize;
q->data[q->rear]=e;
return true;
}
bool deQueue(SqQueue *&q,ElemType &e)
{ if (q->front==q->rear)
return false;
q->front=(q->front+1)%MaxSize;
e=q->data[q->front];
return true;
}
int main()
{
SqQueue *q;
InitQueue(q);
ElemType n,a=2019101701,b=2019101702,c=2019101703,d=2019101704;
enQueue(q,a);
enQueue(q,b);
enQueue(q,c);
enQueue(q,d);
cout<<"请输入排队病人的病历号:";
cin>>n;
cout<<"把病人的病历号加入到队列中(入队)"<<endl;
enQueue(q,n);
printf("病历号为%d的病人已经入队
",n);
cout<<"病历号最前的排队病人现在就诊并将其删除(出队)。。。"<<endl;
deQueue(q,a);
cout<<a<<endl;
cout<<"从队首到队尾列出所有的排队病人病历号(依次出队)"<<endl;
deQueue(q,b);
cout<<b<<endl;
deQueue(q,c);
cout<<c<<endl;
deQueue(q,d);
cout<<d<<endl;
deQueue(q,n);
cout<<n<<endl;
cout<<"不再排队,余下依次就诊并列出所有的排队病人的病历号"<<endl;
deQueue(q,b);
cout<<b<<endl;
deQueue(q,c);
cout<<c<<endl;
deQueue(q,d);
cout<<d<<endl;
deQueue(q,n);
cout<<n<<endl;
cout<<"下班了(释放队列)" <<endl;
DestroyQueue(q);
}