#include <iostream>
#include <stdio.h>
#include<malloc.h>
using namespace std;
typedef int ElemType;
typedef struct qnode{
int data;
struct qnode *next;
}DataNode;
typedef struct{
DataNode *front;
DataNode *rear;
}LinkQuNode;
void initQueue(LinkQuNode *&q){
q=(LinkQuNode *)malloc(sizeof(LinkQuNode));
q->front=q->rear=NULL;
}
void destroyQueue(LinkQuNode *&q){
free(q);
}
bool enQueue(LinkQuNode *&q,ElemType e){
DataNode *p;
p=(DataNode *)malloc(sizeof(DataNode));
p->data=e;
p->next=NULL;
if(q->rear==NULL){
q->front=q->rear=p;
}
else{
q->rear->next=p;
q->rear=p;
}
}
bool deQueue(LinkQuNode *&q,ElemType e){
DataNode *p;
if(q->rear==NULL){
return 0;
}
else{
while(q->rear!=NULL){
p=q->front;
if(q->front==q->rear){
q->front=q->rear=NULL;
}
else{
q->front=q->front->next;
}
cout<<p->data<<" ";
free(p);
}
}
return true;
}
int main(){
LinkQuNode *q;
initQueue(q);
ElemType e,a=1,b=2,c=3,d=4;
enQueue(q,a);
enQueue(q,b);
enQueue(q,c);
enQueue(q,d);
cout<<"请输入排队病人的病历号:";
cin>>e;
cout<<"把病人的病历号入队"<<endl;
enQueue(q,e);
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,e);
cout<<e<<endl;
cout<<"下班了,释放队列"<<endl;
destroyQueue(q);
}