Algorithm (4)--The chain list implementation of the queue

Source: Internet
Author: User

1. The list of queues is implemented enqueue at the tail node, DeQueue in the first node.

1#include"fatal.h"2 3typedefintElementType;4typedefstructQueuenode *Queue;5typedefstructQueuenode *Position;6 7 #defineMAXSIZE (6)8 9 Ten structQueuenode { One ElementType data; A Position Next; - Position Rear; - Position Front; the   intsize; - }; -  - voiddeletequeue (Queue Q); + Queue Makeempty (queue Q); - intIsEmpty (Queue Q); + intisfull (Queue Q); A voidEnQueue (ElementType X, Queue Q); at voidDeQueue (Queue Q); - ElementType Frontanddequeue (Queue); -  -  -  - intMainintargcChar*argv[]) { in Queue Q; -Q =Makeempty (NULL); to  +EnQueue (1, Q); -EnQueue (2, Q); the    *ElementType X = A ; $   Panax Notoginseng    while( !IsEmpty (Q)) { -printf ("%d", q->front->data); the DeQueue (Q); +   } Aprintf"\ n");  theprintf"Empty =%c\n", IsEmpty (Q)?'Y':'N' ); +  -Q =Makeempty (NULL); $ EnQueue (x,q); $EnQueue ( +, Q); - EnQueue (x,q); -printf"Queue is:"); the    -Position P = Q-Front;Wuyi    while(P) { theprintf ("%d",p->data); -P = p->Next; Wu   } -printf"\ n"); About    $printf ("Front and rear:%d%d",q->front->data,q->rear->data); -printf"\ n"); -    -ElementType Y =Frontanddequeue (Q); Aprintf ("Del and front:%d%d", y,q->front->data); +    theprintf"\ n"); -Y =Frontanddequeue (Q); $printf ("Del and front:%d%d", y,q->front->data); theprintf"\ n"); the    theY =Frontanddequeue (Q); theprintf ("Del and front:%d%d\n", Y, (Q->front = = Q?)0:q->front->data)); -  inDeQueue (Q);//Do not Del any node the DeQueue (Q); the  Aboutprintf"size =%d\n",q->size); the  the   return 0; the  + } -  the Bayi voidDelete (Queue Q) { the   if(Q =NULL) theError ("Out of space."); - Position P, tmp; -P = q->Next; theQ-Next =NULL; the  the    while(P! =NULL) { theTMP = PNext; -      Free(P); theP =tmp; the   } the }94  the Queue Makeempty (queue Q) { the   if(Q! =NULL) the Delete (Q);98  AboutQ =malloc(sizeof(structqueuenode)); -   if(Q = = NULL) Error ("Out of memory.");101Q-Next =NULL;102Q-Rear =Q;103Q-Front = q->Next;104Q-> size =0; the   returnQ;106 }107 108 intisfull (Queue Q) {109   returnQ->size = =MAXSIZE; the }111  the intIsEmpty (Queue Q) {113   returnQ->size = =0; the } the  the voidEnQueue (ElementType X, Queue Q) {117   if(Q = = NULL) Error ("Out of space.");118 119 Position tmp; -TMP =malloc(sizeof(structqueuenode));121   if(tmp = = NULL) Error ("Out of space,");122 123TMP--data =X;124Q-Rear Next =tmp; theQ-Rear =tmp;126Q-Rear Next =NULL;127Q-Front = Q-Next; -q->size++;129  the   if(Q-size > MAXSIZE) Error ("Too large.");131    the }133   134 voidDeQueue (Queue Q) {135   if(IsEmpty (Q)) Error ("Empty Queue.");136   137Position tmp = Q-Front;138Q-Next = tmp-Next;139     Free(TMP); $   if(Q-Front = = q->rear) {Q->front =Q;}141   Else{Q-Front = q->Next; }142q->size--;143 }144   145 ElementType frontanddequeue (Queue Q) {146 ElementType X;147   if(IsEmpty (Q)) Error ("Empty Queue.");148   149X = Q->front->data; MaxPosition T = q->Front;151Q-Next = t->Next; the   if(Q-Front = = q->rear) {Q->front =Q;}153   Else{Q-Front = q->Next; }154    Free(t);155Q-size--;156     157   returnX;158}

Algorithm (4)--The chain list implementation of the queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.