Data structure upgraded learning, queue (sequential queue and cyclic queue)
preface:
We finished learning the stack before. It's relatively simple. Today, we learn the sequential queue and circular queue in the queue. It's not difficult to say. It's not easy to say. We need to study carefully. Bloggers will try their best to explain the principle and logic. Children's shoes that don't understand can be read more carefully. Bloggers really want to teach you. I hope you will look at it carefully. If you have any questions, you can comment and put forward them. I hope you like it.
Once a day to prevent puppy love:
1. What is a queue
Queue is a special linear table, which only allows deletion at the front of the table and insertion at the back of the table. Like stack, queue is a linear table with limited operation. The end that performs the insertion operation is called the tail of the queue, and the end that performs the deletion operation is called the head of the queue. When there are no elements in the queue, it is called an empty queue. The data elements of a queue are also called queue elements. Inserting a queue element into a queue is called queue in, and deleting a queue element from a queue is called queue out. Because a queue can only be inserted at one end and deleted at the other end, only the elements that enter the queue first can be deleted from the queue first. Therefore, a queue is also called a FIFO first in first out linear table.
1.1 understand sequence queue and logic
Sequential queue is the sequential storage structure of queue. In fact, sequential queue is a sequential table with limited operation. Like a sequence table, a sequence queue uses a vector space to store the elements in the current queue. Since the positions of the queue head and tail change, set two pointers front and rear to indicate the positions of the queue head element and tail element in the vector space respectively, and their initial values should be set to 0 during queue initialization. The speaker's words are (an array has two flags, one at the head and one at the tail. The head is responsible for the output value and the tail is responsible for the input value. The maximum value of these two values is MaxSize). Roughly, in life, we need to learn its logic to understand the meaning of this thing, so as to facilitate our memory and draw a simple life map for everyone to understand
I don't know if you have ever drunk tea Yan Yuese. The blogger is a college student in Changsha. Tea Yan Yuese is very popular here. Every time the blogger has time to go outside and order a cup of tea Yan Yuese, every time he goes to the blogger is the end of the team. The poor guy has to queue up every time he goes to drink tea Yan Yuese. When the blogger arrives at the head of the team, the blogger will find that there is no one behind me, Humble, order a "Youth" or "Youlan latte", and you will find that you have to wait. When he gives milk tea, you will find that the people before you are also waiting, and in front of you, you have to queue up. Hahaha, when you drink, you will find that the waiting is worth it. (queuing is equivalent to queuing in our life. Those who come first should buy first, and those who come later should queue up and wait for the head of the queue to buy). The sequence queue is relatively simple, that is, the head and tail increase automatically, and then both the head and tail exit conditions are equal to MaxSize. You can directly use the code of the circular queue to express the sequence table, because the maximum value of the circular queue (sq.front+1)%MaxSize and MaxSize are smaller than 1, so we won't show it. The focus is the circular queue.
1.2 understand the circular queue and the code we need
In order to make full use of vector space and overcome the phenomenon of "false overflow", the vector space is imagined as a ring with head and tail connected, and this vector is called cyclic vector. The queue stored in it is called Circular Queue. Circular Queue is to connect the sequential queue end to end, and logically regard the table storing queue elements as a ring to become a Circular Queue.
It is equivalent to always turning in this queue, which can easily save space
#define MaxSize 20 / / the maximum number of storage in our queue typedef struct { int data[MaxSize]; int front,rear; }SqQueue;//Circular queues are of type sq.rear =sq.front = 0;//Initialize queue head and tail (sq.rear+1)%MaxSize==sq.front//This is the overflow when the team is full. As long as we enter the team, we can continue to leave the team at the initial place sq.rear==sq.front//This sentence is "team air spilling down" sq.rear = (sq.rear+1)%MaxSize;//Entering the team module division is equivalent to looping into one, which is equivalent to deviating from a position in the future. When it is full, it will reach the head of the team, because module division *x =sq.data[(sq.front+1)%MaxSize];//Getting out of the team is the same as getting in the team
The above is the core code of circular queue.
1.3 cycle queue initialization
typedef struct { int data[MaxSize];//Define queue space size int front,rear;//Team leader and tail }SqQueue; void InitQueue(SqQueue &sq) { sq.rear =sq.front = 0;//initialization } int GetHead(SqQueue sq,int *x)//Gets the value of the queue head { if(sq.rear==sq.front)//Team air { return 0; } *x =sq.data[(sq.front+1)%MaxSize];//Take the value of the queue head return 1; }
1.4 cyclic queue entry
int EnQueue(SqQueue &sq,int x)//Join the team { if((sq.rear+1)%MaxSize==sq.front)//Judge team full { return 0; } sq.rear = (sq.rear+1)%MaxSize;//It is equivalent to deviating upward from a position. When it is full, it will reach the initial position, because the module division cycle sq.data[sq.rear] = x;//Write value to return 1; }
1... 5 cycle queue out
int DeQueue(SqQueue &sq,int *x) { if(sq.rear==sq.front){//Judge team empty return 0; } sq.front = (sq.front+1)%MaxSize;//It's equivalent to deviating from a position downward. When it's full, it will reach the initial position, because the module is divided *x =sq.data[sq.front]; return 1; }
1.6 circular queue effect demonstration and overall code
#include <stdio.h> #include <stdlib.h> #define MaxSize 6 typedef struct { int data[MaxSize]; int front,rear; }SqQueue; void InitQueue(SqQueue &sq) { sq.rear =sq.front = 0; } int EnQueue(SqQueue &sq,int x) { if((sq.rear+1)%MaxSize==sq.front) { return 0; } sq.rear = (sq.rear+1)%MaxSize; sq.data[sq.rear] = x; return 1; } int DeQueue(SqQueue &sq,int *x) { if(sq.rear==sq.front){ return 0; } sq.front = (sq.front+1)%MaxSize; *x =sq.data[sq.front]; return 1; } int GetHead(SqQueue sq,int *x) { if(sq.rear==sq.front) { return 0; } *x =sq.data[(sq.front+1)%MaxSize]; return 1; } int QueueEmpty(SqQueue sq) { if(sq.rear==sq.front)return 1; return 0; } int main(int argc, char *argv[]) { SqQueue sq; int e; int i,n,m,num; printf("Initialize queue\n"); InitQueue(sq); if(QueueEmpty(sq)==1) { printf("Team air\n"); } else{ printf("The team is not empty\n"); } printf("Enter the number of you need to join the team(No more than MaXSize: %d)\n",MaxSize); scanf("%d",&n); printf("Enter your%d Value of:",n); for(i=0;i<n;i++) { scanf("%d",&num); if(EnQueue(sq,num)) { printf("%d Team success\n",num); } else{ printf("%d If you fail to join the team, the team may be full\n",num); } if(i==4){ DeQueue(sq,&e); printf("Out of the team:%3d\n",e);//When the team is full, one can enter the team again. The sequence table is not allowed } } GetHead(sq,&e); printf("After joining the team, the value of the team head is:%d\n",e); printf("Enter the number of queues(No more than%d number)\n",n); scanf("%d",&m); printf("Outgoing value:") ; for(i=0;i<m;i++) { if(DeQueue(sq,&e)==1) { printf("%3d",e); } else { printf("Team air"); } } return 0; }
Note: the blogger's code does not use a pointer, so an error will be reported. You need to change it to a. cpp file, or you can change the pointer and use "- >" to access
Summary:
In fact, the difficulty of the queue is good, and there are similarities and differences between the queue and the stack. As long as we understand the logic, it is easy to write it. The premise is that you know the meaning of the code, and you can write your logic with the code. The circular queue is equivalent to a rotating memory, like the two ends of the water pipe of life, and then the water in it rotates constantly. It is not easy to create, praise, Attention, comments, collection, thank you. If you don't give it, you'll cry and show it to you!!!