#include <stdio.h>#include<stdlib.h>#include<malloc.h>#defineTRUE 1#defineFALSE 0#defineOK 1#defineERROR 0#defineINFEASIBLE-1#defineOVERFLOW-2typedefintStatus;typedefintElemtype;typedefstructqnode{elemtype data; structQnode *Next;} Qnode,*Ptr;typedefstruct{ptr head; PTR tail;} Linkqueue;status Initqueue (Linkqueue&Q) {//Initialize QueueQ.head=q.tail= (PTR)malloc(sizeof(Qnode)); if(!q.head) exit (OVERFLOW); Q.head->next=NULL; returnOK;} Status Enqueue (Linkqueue&q,elemtype e) {//inserting elements into a queuePTR p= (PTR)malloc(sizeof(Qnode)); if(!p) exit (OVERFLOW); P->next=null,p->data=e; Q.tail->next=p; Q.tail=p; returnOK;} Status Dequeue (Linkqueue&q,elemtype &e) {//Delete team first element if(q.head==q.tail)returnERROR; PTR P= (PTR)malloc(sizeof(Qnode)); P=q.head->Next; E=p->data; Q.head->next=p->Next; if(q.tail==p) Q.tail=Q.head; Free(P); returnOK;} Status Destroy (Linkqueue&Q) {//destroying a queue chain list while(q.head) {Q.tail=q.head->Next; Free(Q.head); Q.head=Q.tail; } returnOK;}intGet_len (Linkqueue q) {//Get Queue Length intCnt=0; while(q.head->next!=NULL) {Q.tail=q.head->Next; CNT++; Q.head=Q.tail; } returnCNT;} Status print (Linkqueue q) {//output Linear table elements while(q.head->next!=NULL) {Q.tail=q.head->Next; printf ("%d",q.tail->data); Q.head=Q.tail; } printf ("\ n"); returnOK;} Status GetHead (Linkqueue q,elemtype&e) {//get the first element of the teamPTR p= (PTR)malloc(sizeof(Qnode)); P=q.head->Next; E=p->data; returnOK;} Status Qempty (Linkqueue q) {//determines whether the empty if(q.head==q.tail)returnTRUE; Else returnFALSE;} Status Clearqueue (Linkqueue&Q) {//clear Q to empty queueptr p1,p2; Q.tail=Q.head; P1=q.head->Next; Q.head->next=NULL; while(p1) {P2=P1; P1=p1->Next; Free(p2); } returnOK;}intMain () {Linkqueue q; Initqueue (q); Elemtype e; for(intI=1; i<=5; i++) {enqueue (q,i); } gethead (Q,e); printf ("%d\n", e);//Output Team first elementprint (q); printf ("%d\n", Get_len (q)); Dequeue (q,e); Enqueue (q,Ten); Print (q); printf ("%d\n", Get_len (q)); intFlag=qempty (q);//deciding whether to use when emptyprintf"%d\n", flag); Clearqueue (q);//use when emptying the queue return 0;}
Chained queue templates