Data structure learning is interesting, sincerely recommend Hao Bin Teacher's data structure video, really explain the very detailed, easy to understand.
has been following Hao bin teacher's data structure video learning, watching the queue of video, recorded, summed up.
Queue definition: A queue is a special linear table that allows deletions only at the head of the table (front), and a linear data structure that inserts operations at the end of the table (rear), which is called a queue. One end of the insert operation is called the tail of the queue, and one end of the delete operation is called the tail.
Types of queues: Chained queues, which are queues implemented with linked lists. Static queues: Queues that are implemented with arrays. Here, we take a static queue implemented with arrays. Because the queue implemented with the list is a dynamic queue, it is easier to operate, so we are using a static queue.
In the static queue, the Hao bin teacher is talking about a more complex static loop queue. The explanation here is that if we're using a static queue, it's bound to involve arrays, and if we're constantly deleting data from the queue, the head of our queue (front) will go up and down (here, front always point to the head element in the queue, Rear always points to the next element of the last element of the queue, so that the location we are removed from can never be exploited again, which is a waste of space, so if so, the data structure of the queue loses its original meaning. Therefore, static queues that are implemented with static arrays are generally static circular queues. So, the question is, since it is a static loop queue, when is the queue empty? When is the queue full?
Here first of all, when the queue to be empty, according to our previous explanation, the queue is deleted, front will continue to move upward, as follows:
As above, in the initial state front and rear are in 0 position, when inserting elements, rear will go up, then fill in an element 0 of the position will be filled in, when filling in an element after the 1 position is filled. At this point the rear is at 2, front at 0, and there are two elements in the queue at 0 and 1 respectively. So, how is the queue empty? It is simple to keep deleting elements, and when the elements are all erased, the current queue is empty. Delete the 0 element, front will move one position to 1, then delete an element front will move one position to 2, when no element exists in the entire queue, the entire queue is empty. Therefore, the condition that indicates whether the queue is empty is that front and rear are in one place.
So when does it mean that the current queue is full? As you may have guessed, you might think that when front and rear are in the same position, the entire queue can also indicate that the current queue is full. That is the constant insertion of the elements, rear constantly moving the position, until the rear again moved to 0, all the positions are filled with the elements, the entire queue is full. Yes, the entire queue is indeed full at this time. But this will repeat the condition that our queue is empty, so here are two ways to fix this, and we can define a value that represents the count of counts, representing the number of elements in the current queue. If front and rear are in the same location, and count is 0, the queue is empty. If front and rear are in the same location, and count is not 9, the current queue is full. Another method is that we do not use the entire default, we use n-1 elements, so that when the rear and front in the "next to" position, it means that the current queue is full, and in the subscript, there is no strict front and rear size relationship.
Well, if everyone in front of you already understand, then you can implement the basic operation of the queue (the code has comments, I will no longer explain in detail).
Definition of the queue:
1 /*2 Defining Queues3 A queue is a data structure that can only be deleted in a paragraph. 4 the static loop queue is used here, so the queue is implemented using an array5 */6typedefstructqueue{7 int* PBASE;//used to store arrays8 intFront//To represent the current front, point to the queue header9 intRear//to represent the next node that is currently rear, pointing to the tail of the queue.Ten}queue;
Initialization of the queue: Here you can also pass the length of the queue as a parameter, and I'll write it directly here.
1 /*2 Queue initialization Function3 Initialize an empty queue to give the queue a length, and there are no values in it4 */5 voidInit (QUEUE *PQ) {6Pq->pbase= (int*)malloc(sizeof(int)*6);//give a queue array of 6 shaped lengths7Pq->front=0;8Pq->rear=0;//initialize the queue to null9}
Queued functions for queues:
1 /*2 A function that determines whether the current queue is full3 because the default is that when a queue of n is already n-1, it is full and does not allow it to be stored again, so4 Judging whether the queue is full is to determine whether rear and front is already "next to each other", that is, rear in the mobile one will and front coincident. 5 */6 intFull_queue (Queue *PQ) {7 if((pq->rear+1)%6==pq->front) {8 return 0;9 }Ten Else{ One return 1; A } - } - the /* - Queue function - according to the characteristics of the queue, the queue can only be at the end of the queues, that is, rear, so the algorithm of the queued function is to put the inserted data in the rear position - after the insert completes, rear moves backward one bit, so that rear always points to the next element of the last element in the queue. + Here's a point to note: What if the current rear position is already the last position of the array when we move the rear position? - should let rear back to 0, because our queue is set by default is the loop queue, that is, our queue can turn, when the queue is not full, + but rear is already in the last position of the queue, so we should be moving the queue to the 0 position. This situation in mathematics is to add 1 to take the surplus. A */ at intEn_queue (Queue *pq,intval) { - if(Full_queue (pQ) = =0){ - return 0; - } - Else{ -pq->pbase[pq->rear]=Val; inPq->rear= (pq->rear+1)%6; - return 1; to } +}
The traversal function of the queue:
1 /*2 functions that traverse the queue3 because this is just traversing a queue, we cannot break the original queue structure, so we must define a new I to represent the current subscript. 4 The syntax of traversal is very simple, which is to start from the beginning until the end of the queue, that is, I to the next node of the last node (rear), the traversal is the end. 5 */6 voidTraverse_queue (Queue *PQ) {7 inti=pq->front;//make I traverse from "head" to start8 9 while(i!=pq->rear) {Tenprintf"%d",pq->pbase[i]); OneI= (i+1)%6;//move I down one position A } -}
Out of the queue function:
1 /*2 function of whether the current queue is empty3 */4 intEmpty_queue (Queue *PQ) {5 if(pq->front==pq->rear) {6 return 0;7 }8 Else{9 return 1;Ten } One } A - /* - out Team function the since the queue function has broken down the original data structure, there is no need to redefine a new subscript - The algorithm for the out-of-line function is very simple, which is to delete at the head of the queue (front) and move the front down one position after deletion. - */ - intOut_quene (QUEUE *PQ,int*pVal) { + if(Empty_queue (pQ) = =0){ - return 1;//1 indicates that the current queue is empty + } A Else{ at*pval=pq->pbase[pq->Front]; -Pq->front= (pq->front+1)%6; - return 0;//0 indicates that the current queue is currently successful - } -}
This is the definition of the queue and the code implementation of the basic operation.
This article is original, if reproduced please indicate the source.
Queue definition and basic operation realization of data structure