About circular queues
Loop queue is like a circle, you can keep on the queue and out of the team, for example: Queues are full, if you perform a team operation, the queue head is empty, this time you can continue to insert elements empty there, the head pointer back to the second element becomes the head of the queue, the last opponent becomes the tail of the team
Medium: Now the queue is full, but when the 12 out of the team, the head pointer will point to the 1th position, this is to forget the queue inserted elements, tail will point to 0 position, and then insert the element into the 0 position.
Elements that make up the loop queue
int data[COUNT];//存放元素int head;//头指针int tail;//尾指针intcount;//队列中的元素个数,判断队列是否空或者满的重要作用
Case of change of head and tail pointers
- When the queue is empty, both the head and tail pointers are-1;
- During the queue operation, the tail pointer moves backward one bit, then inserts the data, count++
- When the team is out of operation, the head pointer moves backwards, and the data refers to the elements of the team, count–
- Head pointer change
cq->head = (cq->head + 1) % COUNT;
- Tail pointer change
cq->tail = (cq->tail + 1) % COUNT;
Implementing circular queues using C language
#include <stdio.h>#define COUNT 10//The maximum number of cyclic queues that can be storedtypedef struct {intData[count];//Data area intHead//Head pointer intTail//Tail pointer int Count;//Current number of data in the queue}cyclequeue;/ * Queue Operation +*/voidInsert (Cyclequeue *cq,intNUM) {if(cq->Count= = COUNT) {printf ("The queue is now full \ n"); }Else{Cq->tail = (Cq->tail +1)% COUNT;//Cycle queue, move backward one position, if the value of tail is count, then tail will automatically become 0Cq->data[cq->tail] = num; Cq->Count++; }}/ * Exit operation, Temp is the element of the stack * /intDelete (Cyclequeue *cq,int*temp) {if(cq->Count==0) {printf ("The queue is empty!" \ n ");return 0;//out of stack failure}Else{Cq->head = (Cq->head +1)% COUNT;//Cycle queue, move backward one position*temp = cq->data[cq->head];//Assign the element that will be out of the team to tempCq->Count--;//number of elements in the queue-1 return 1;//Complete the team}return 1;}intMain () {Cyclequeue CQ; Cq.head = Cq.tail =-1; Cq.Count=0;inttemp =0;//stores the elements of the queueInsert (&CQ,1); Insert (&CQ,2); Insert (&CQ,3); Insert (&CQ,4); Insert (&CQ,5);int Count= CQ.Count; for(inti =0; I <Count; i++) {Delete (&CQ, &temp); printf"Out of team element for%d\n", temp); }return 0;}
"Data structure-queue" loop queue