编辑代码

#include <iostream>
#include <stdio.h>
#include<malloc.h>
using namespace std;
typedef int ElemType;
typedef struct qnode{//队数据结点的类型DataNode声明
int data;
struct qnode *next;
}DataNode;
typedef struct{//队头节点的类型LinkQuNode的声明
    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));//创建新结点,存放元素e(由p指向它)
     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);

}