"Data structure-queue" loop queue

Source: Internet
Author: User

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 changecq->head = (cq->head + 1) % COUNT;
    • Tail pointer changecq->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 (&AMP;CQ,1); Insert (&AMP;CQ,2); Insert (&AMP;CQ,3); Insert (&AMP;CQ,4); Insert (&AMP;CQ,5);int Count= CQ.Count; for(inti =0; I <Count;        i++) {Delete (&AMP;CQ, &temp); printf"Out of team element for%d\n", temp); }return 0;}

"Data structure-queue" loop 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.