Original address: http://blog.csdn.net/hguisu/article/details/7674195
1#include"stdafx.h"2#include"stdlib.h"3#include <stdio.h>4#include <iostream>5 using namespacestd;6 7 #defineTRUE 18 #defineFALSE 09 #defineOK 1Ten #defineERROR 0 One #defineINFEASIBLE-1 A #defineOVERFLOW-2 - #defineQUEUEEMPTY-3 - the -typedefintStatus; -typedefintElemtype; - +typedefstructlnode{ - elemtype data; + structlnode*Next; A}lnode,*linklist; at -typedefstructqueue{ - linklist Front; - linklist rear; - }queue; - in voidInitqueue (QUEUE *Q); - voidEnQueue (QUEUE *q,elemtype elem); to voidDeQueue (QUEUE *q,elemtype *elem); + voidGetfront (QUEUE Q,elemtype *elem); - BOOLqueueempty (QUEUE Q); the intfreequeue (QUEUE Q); * $ //Initialize QueuePanax Notoginseng voidInitqueue (QUEUE *Q) - { theQ->front = (linklist)malloc(sizeof(Lnode)); + if(Q->front = =NULL) exit (ERROR); AQ->rear = q->Front; the } + - //Queue $ voidEnQueue (QUEUE *q,elemtype Elem) $ { - linklist S; -s = (linklist)malloc(sizeof(Lnode)); the if(!s) exit (ERROR); -S->data =Elem;WuyiS->next =NULL; theQ->rear->next =s; -Q->rear =s; Wu } - About //out Team $ voidDeQueue (QUEUE *q,elemtype *elem) - { - linklist S; - if(Queueempty (*Q) exit (ERROR); A*elem = q->front->next->data; +s = q->front->Next; theQ->front->next = s->Next; - Free(s); $ } the the //get Team Header element content the voidGetfront (QUEUE Q,elemtype *elem) the { - if(Queueempty (Q)) exit (ERROR); in*elem = q.front->next->data; the } the About //determine if the queue Q is empty the BOOLqueueempty (QUEUE Q) the { the if(Q.front = = q.rear)returnTRUE; + returnFALSE; - } the Bayi //Release Queue the intFreequeue (QUEUE *Q) the { - while(q->front) - { theQ->rear = q->front->Next; the Free(q->front); theQ->front = q->Rear; the } - returnOK; the } the the int_tmain (intARGC, _tchar*argv[])94 { the QUEUE Q; theInitqueue (&Q); theEnQueue (&q,1);98EnQueue (&q,2); About elemtype e; -DeQueue (&Q,&e);101cout <<"De Queue:"<<e;102 103Freequeue (&Q);104 theDeQueue (&Q,&e);106cout <<"De Queue:"<<e;107System"Pause");108 return 0;109}
Queues--chained queues