Data Structures-sequential loop queues and chained queues

Source: Internet
Author: User

The first part: the implementation of sequential cyclic queue

1 //implementation of the cyclic queue2 #defineOK 13 #defineMaxsize_q 104 //#define OVERFLOW-25 #defineERROR 06 7typedefintStatus;8typedefintQelemtype;9 Tentypedefstruct{ OneQelemtype *Base;//dynamically allocated storage space for initialization A     intFront//Team head Pointer -     intRear//Team Tail Hands -  the }sqqueue; -  - //---------------------The algorithm description of the basic operation of the cyclic queue----------------------------- -Status Initqueue (Sqqueue *q) + { -     //constructs an empty queue Q +Q->Base= (qelemtype*)malloc(maxsize_q*sizeof(Qelemtype)); A     if(!q->Base) Exit (-1);//Storage Allocation Failure atQ->front=q->rear=NULL; -     returnOK; - } -  - intqueuelength (sqqueue q) - { in     //returns the number of elements in Q, the length of the queue -     return(q.rear-q.front+maxsize_q)%maxsize_q; to } +  - BOOLIsfull (Sqqueue *q) the { *     if(Q->front= (q->rear+1)%maxsize_q) $         return true;Panax Notoginseng     return false; - } the  + BOOLIsEmpty (Sqqueue *q) A { the     if(q->front==q->rear) +         return true; -     return false; $ } $  -Status EnQueue (Sqqueue *q,qelemtype e) - { the     //Insert element E for Q's new team tail element -     if((q->rear+1)%maxsize_q==q->front)Wuyi     { the         //queue full, no action, no more queues -         returnERROR; Wu     } -Q->Base[q->rear]=e; About     //working on the queue $Q->rear= (q->rear+1)%maxsize_q; -     returnOK; - } -  AStatus DeQueue (Sqqueue *q,qelemtype *e) + { the     //if the queue is not empty, delete the team header element of Q, return the value with E, and return OK, otherwise return error -     if(Q->rear==q->front)returnERROR;//Queue Full $*e=q->Base[q->front];//Find the deleted team header element theQ->front= (q->front+1)%maxsize_q; the     returnOK; the  the } -  in voidDisplayqueue (Sqqueue *q) the { the     if(q->front==q->rear) About     { theprintf"queue is full \ n"); the     } the     Else{ +         //Traverse Queue -         intFront=q->Front; the         intRear=q->Rear;Bayi          while(front!=rear) the         { theprintf"%d\n",q->Base[Front]); -front++; -         } the     } the}

Test Cases

1 intMain ()2 {3 Sqqueue Q;4Initqueue (&q);5     inti;6      for(i=0;i< One; i++){7         if(EnQueue (&q,i) = =ERROR) {8printf"the loop queue is full and the queue length is 9 \ n");9         }Ten     } Oneprintf"The length of the queue is%d \ n", Queuelength (q));//The length of the queue is 9 A     intE1,e2,e3,e4,e5; -DeQueue (&q,&E1); -DeQueue (&q,&E2); theDeQueue (&q,&e3); -DeQueue (&q,&e4); -DeQueue (&q,&e5); -printf"%d--%d--%d--%d--%d \ n", e1,e2,e3,e4,e5); +printf"The length of the queue is%d \ n", Queuelength (q)); -printf"traversal of the cyclic queue \ n"); +Displayqueue (&q); A     inte6; atDeQueue (&q,&e6); -printf"traversal of the cyclic queue \ n"); -Displayqueue (&q); -System"Pause"); -     return 0; -}

Data Structures-sequential loop queues and chained queues

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.