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