Sequential queue of two-step algorithm

Source: Internet
Author: User

The sequential list code is very simple;

However, this code will have a false overflow of the situation appear;

is in the end of the team is full of circumstances, constantly out of the team after

If the queue at this time to determine the full condition is q->head==q->tail this condition is satisfied at this time. But the actual queue has been out of several teams before, there is still space in front, which is false overflow; (forgive me for drawing)

False Overflow Solution

1. Move all data forward each time you exit the team. This method is too inefficient to move large amounts of data.

2. Use the loop queue. This method will be played again tomorrow.

1 #defineQueuemax 152typedefstruct 3 {4DATA Data[queuemax];//Queue Array5     intHead//Team Head6     intTail//End of Team7 }seqqueue;8 9 Ten  OneSeqqueue *seqqueueinit ()//Initialize Queue A { -Seqqueue *Q; -     if(Q= (seqqueue*)malloc(sizeof(Seqqueue)))//Allocating Memory the     { -Q->head=0; -Q->tail=0; -         returnQ; +     } -     Else +         returnNULL; A } at  - voidSeqqueuefree (Seqqueue *q) - { -     if(q!=NULL) -          Free(q); - } in  - intSeqqueueisempty (Seqqueue *q) to { +     return(Q->head==q->tail);//Check if the queue is empty - } the  * intSeqqueueisfull (Seqqueue *q) $ {Panax Notoginseng     return(q->tail==Queuemax);  - } the  + intSeqqueuelen (Seqqueue *q)//Get Queue Length A { the     return(q->tail-q->head); + } -  $ intSeqqueuein (seqqueue *q,data DATA)//Queued Operation $ { -     if(q->tail==Queuemax) -     { theprintf"The queue is full! \ n"); -         return 0;Wuyi     } the     Else -     { Wuq->data[q->tail]=data; -tail++; About         return 1; $     } - } -  -DATA *seqqueueout (Seqqueue *q)//out of team Operation A { +     if(q->head==q->tail) the     { -printf"the queue is empty! \ n"); $         returnNULL; the     } the     Else the     { the         return& (q->data[q->head++]); -     } in } the  theDATA *seqqueuepeek (Seqqueue *q)//get team head element About { the     if(Seqqueueisempty (q)) the     { theprintf"the queue is empty! \ n"); +         returnNULL; -     } the     ElseBayi     { the         return& (q->data[q->Head]); the     } -}

Sequential queue of two-step algorithm

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.