To implement a queue operation with a linked list, the code is as follows:
1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <malloc.h>5 6 using namespacestd;7 8 //function Status Code definition9 #defineTRUE 1Ten #defineFALSE 0 One #defineOK 1 A #defineERROR 0 - #defineINFEASIBLE-1 - #defineOVERFLOW-2 the -typedefintStatus; -typedefintQelemtype; - +typedefstructQnode { - qelemtype data; + structQnode *Next; A}qnode, *queueptr; at -typedefstruct { - queueptr Front; - queueptr Rear; - }linkqueue; - inStatus Initqueue (Linkqueue &Q) { -Q.front = Q.rear = (queueptr)malloc(sizeof(Qnode)); to if(!Q.front) exit (OVERFLOW); +Q.front->next =NULL; - returnOK; the } * $Status Destroyqueue (Linkqueue &Q) {Panax Notoginseng while(q.front) { -cout << Q.front->data <<Endl; theQ.rear = q.front->Next; + Free(Q.front); AQ.front =q.rear; the } + returnOK; - } $ $Status EnQueue (Linkqueue &Q, Qelemtype e) { -Q.rear->next = (queueptr)malloc(sizeof(Qnode)); - if(! Q.rear->next) exit (OVERFLOW); theQ.rear->data =e; -Q.rear = q.rear->Next;WuyiQ.rear->next =NULL; the returnOK; - } Wu -Status DeQueue (Linkqueue &q, Qelemtype &e) { About if(Q.front = =q.rear) $ returnERROR; -E = q.front->data; -Queueptr p =Q.front; -Q.front = q.front->Next; A Free(p); + returnOK; the } - $ BOOLEmptyqueue (Linkqueue &Q) { the returnQ.front = =q.rear; the } the the intMain () - { in linkqueue A, B; the Initqueue (A); the Initqueue (B); About intN, M; theCIN >>N; the for(inti =0; I < n; ++i) { theCIN >>m; + if(M &1) - EnQueue (A, m); the ElseBayi EnQueue (B, m); the } the BOOLOK =false; - while(! Emptyqueue (A) &&!Emptyqueue (B)) { - DeQueue (A, m); the if(OK) thecout <<' '<<m; the Else thecout <<m; - the if(!Emptyqueue (A)) { the DeQueue (A, m); thecout <<' '<<m;94 } the DeQueue (B, m); thecout <<' '<<m; theOK =true;98 } About while(!Emptyqueue (A)) { - DeQueue (A, m);101 if(OK)102cout <<' '<<m;103 Else104cout <<m; theOK =true;106 }107 while(!Emptyqueue (B)) {108 DeQueue (B, m);109 if(OK) thecout <<' '<<m;111 Else thecout <<m;113OK =true; the } the return 0; the}
PTA 7-1 Bank Business Queue Simple simulation