队列的链式存储

#include<stdio.h>
#include<malloc.h>
#define ElemType int


typedef struct qNode{队列节点结构
ElemType data;
struct qNode *next;
};


typedef struct LinkQueue{队列结构
struct qNode *rear,*front;
};


LinkQueue Init_Queue(LinkQueue &Q){初始化一个带头结点的队列
Q.rear=(qNode *)malloc(sizeof(qNode));
Q.rear->next=NULL;
Q.front=Q.rear;
return Q;
}


LinkQueue Enter_Queue(LinkQueue &Q,ElemType e){//入队操作
qNode *p;
p=(qNode *)malloc(sizeof(qNode));
if(!p){
printf("overflow");
}else{
//printf("%dwo",e);//为了检错
p->data=e;
p->next=Q.rear->next;
Q.rear->next=p;
Q.rear=p;
//printf("%dwo",Q.rear->data);
}
return Q;
}


LinkQueue Leave_Queue(LinkQueue &Q){//出队
qNode *p;
int e;
p=Q.front->next;
if(p==NULL){
printf("队空");
}else{
if(p==Q.rear){
Q.rear=Q.front;
}else{
e=p->data;
printf("%d ",e);
Q.front->next=p->next;
free(p);
}
}
return Q;
}


void Destroy_Queue(LinkQueue &Q){//销毁队列
qNode *p;
while(Q.front){
p=Q.front;
Q.front=p->next;
free(p);
}
if(!Q.front){
printf("队列销毁 ");
}
}


int main(){
LinkQueue Q;
int a,e;
Q=Init_Queue(Q);
printf("输入元素 ");
scanf("%d",&a);
while(a!=-1){
Q=Enter_Queue(Q,a);//入队操作
scanf("%d",&a);
}
while(Q.front->next!=NULL){
Q=Leave_Queue(Q);//出队操作
}
Destroy_Queue(Q);//销毁队列

return 0;
}

原文地址:https://www.cnblogs.com/jiafeng1996/p/11361458.html