Today to see the breadth of the first traversal of the graph, found the use of the loop queue, fill in the knowledge of the circular queue, referring to the "Big talk data structure" p116~117, wrote a simple test example for easy understanding.
First, you need to understand the following three formulas.
Front is the subscript for the head element of the team, and rear is the subscript behind the tail element of the team. (The book with the head and tail hands,front and rear is not a pointer, personally feel not very good)
1, the queue empty conditions
Apparently front==rear
Note: If the queue does not preserve any element space
In the case of front==rear, the queue may be empty or the queue may be full. So for convenience, This article discusses the method of preserving an element space . (You can also use the method of setting the flag variable)
2. Conditions of full queue
There are only two cases in which the queue is full, so the conditions for the queue full are:
(rear+1)%queuesize==front
3, the queue Length calculation formula
Considering both cases, the queue calculation formula is:
Rear-front+maxsize)%maxsize
MAXSIZE is the queue Length (including the reserved element space)
Here's a simple example of how to create a loop queue, Enqueue, and team out.
The code and explanation are as follows (Qt creator test pass):
1#include <iostream>2#include <string>3 using namespacestd;4 5 #defineMAXSIZE 56 7 //sequential storage structure for circular queues8typedefstruct9 {Ten CharData[maxsize];//queue, expressed as an array, assuming a maximum of 4 elements One //Keep an element space, otherwise front==rear, is empty queue or full queue is not easy to judge A intFront//header ordinal, subscript of the first element - intRear//tail number, subscript of the last element - }sqqueue; the - //initializes the loop queue, returning the address that points to the loop queue -Sqqueue *initqueue (Sqqueue *Q) - { +q=NewSqqueue; -Q->front=0; +Q->rear=0;//Initialize to empty queue (Front==rear to empty queue) A returnQ; at } - - //to find the length of the loop queue, returning the current length of the loop queue - intQueuelength (Sqqueue *Q) - { - return(q->rear-q->front+maxsize)%MAXSIZE; in } - to //Inbound queue operations for cyclic queues + voidEnQueue (Sqqueue *q,Chare) - { the if((q->rear+1)%maxsize==q->front)//determine if the loop queue is full * { $cout<<"Error"<<" ";Panax Notoginseng return; - } theq->data[q->rear]=e;//Insert element E into the tail of the team +Q->rear= (q->rear+1)%maxsize;//tail number rear plus 1, if the final turn to the head Acout<<"OK"<<" "; the return; + } - $ //out-of-queue operations for cyclic queues $ CharDeQueue (Sqqueue *Q) - { - Chare; the if(q->front==q->rear)//if the queue is empty - {Wuyicout<<"Error"<<" "; the return 0;//returns 0 indicates that the queue is empty and there are no out-of-band elements - } WuE=q->data[q->Front]; -Q->front= (q->front+1)%maxsize;//first serial number front plus 1, if the final turn to the head Aboutcout<<"OK"<<" "<<e<<Endl; $ returne; - } - - intMainvoid) A { +Sqqueue *s=NULL; the - //initialize an empty queue s $s=Initqueue (s); the the //inserts an element and returns the current queue Length theEnQueue (s),'A');//Insert a thecout<<s->data[0]<<" ";//indicates if the end of the queue is inserted as a (since the initialization is rear=0, so start inserting from the subscript 0) -Cout<<queuelength (s) <<endl;//output Current length 1 in theEnQueue (s),'B');//Insert B thecout<<s->data[1]<<" ";//indicates if the end of the queue is inserted b AboutCout<<queuelength (s) <<endl;//output Current length 2 the theEnQueue (s),'C');//Insert C thecout<<s->data[2]<<" ";//indicates if the end of the queue is inserted C success +Cout<<queuelength (s) <<endl;//output Current length 3 - theEnQueue (s),'D');//Insert DBayicout<<s->data[3]<<" ";//Tip Whether the end of the queue is inserted D success theCout<<queuelength (s) <<endl;//output Current Length 4 the -EnQueue (s),'E');//Insert E, output error, indicating insertion failure -Cout<<queuelength (s) <<endl;//output Current length 4, indicating that it is full and cannot be inserted again the the //out Team theDeQueue (s);//output a theDeQueue (s);//Output B - the //re-insert elements to understand the loop of the queue theEnQueue (s),'E');//Insert E thecout<<s->data[4]<<" "<<s->rear<<endl;//indicates if the end of the team is inserted e is successful, this time rear moved to subscript 0 Place94EnQueue (s),'F');//Insert F thecout<<s->data[0]<<" "<<s->rear<<endl;//indicates if the end of the team is inserted F success, this time rear moved to subscript 1 place theEnQueue (s),'G');//Insert g, output error, indicating insertion failure theCout<<queuelength (s) <<endl;//output Current length 4, indicating that it is full and cannot be inserted again98}
Operation Result:
Sequential storage of circular queues and queued outbound operations