链队列题目:初始化队列+入队列+出队列+销毁队列 (1)初始化一个链队列;(2)在初始化好的链队列中放入数,入队列,完成后要求显示;(3)从队列中出队列,要求显示出来的元素和之后的队列;(4)销毁创建的队列,释放内存;#include#include#define NULL 0typedef int QElemType;typedef struct QNode{QElemType data;struct QNode *next;} QNode,*QueuePtr;typedef struct{QueuePtr front;QueuePtr rear;}LinkQueue;void CreateQueue(LinkQueue *Q){ int a;QueuePtr p;p=(QueuePtr)malloc(sizeof(QNode)); if(!p) printf("创建失败") ;else {p->next=NULL; Q->front=p; Q->rear=p; scanf("%d",&a); while(a!=-1) {p=(QueuePtr)malloc(sizeof(QNode));p->next=NULL; if(!p) printf("创建失败") ; else { p->data=a; Q->rear->next=p; Q->rear=p; scanf("%d",&a); } } }}void PrintfQueue(LinkQueue *Q){ QueuePtr p;for(p=Q->front->next;p!=NULL;p=p->next) { printf(" %d",p->data); }}void EnQueue(LinkQueue *Q,QElemType x) { QueuePtr p; p=(QueuePtr)malloc(sizeof(QNode)); if(!p) printf("创建失败") ;else { p->data=x;p->next=NULL;Q->rear->next=p;Q->rear=p;}}void DeQueue(LinkQueue *Q,QElemType *f){ QueuePtr p;if(Q->front==Q->rear) printf("\nError");else { p=Q->front->next; *f=p->data; Q->front->next=p->next;if(Q->rear==p) Q->rear=Q->front;free(p);}}
void DeleteQueue(LinkQueue *Q){QueuePtr p; for(;Q->front!=NULL;free(p)) { p=Q->front; Q->front=Q->front->next;}
}void main(){ int *f,e; LinkQueue Queue,*Q; e=0;f=&e;Q=&Queue;printf("\n输入队列的元素依次为:");CreateQueue(Q);printf("\n初始队列为:");PrintfQueue(Q);printf("\n要入队的元素为:");scanf("%d",&e);EnQueue(Q,e);printf("\n入队后的队列为:");PrintfQueue(Q);DeQueue(Q,f);printf("\n出队的元素为:");printf("%d",e); printf("\n出队后的队列为:");PrintfQueue(Q);DeleteQueue(Q);}