Data structure, data structure and Algorithm
Basic concepts of queue
1. Basic concepts of queue
Queue: it is also a linear table with limited operations. It is a linear table of First In First Out (FIFO. You can only insert at one end of the table and delete at the other end.
Front: the end that is allowed to be deleted is called the front.
Team end (rear): the end that is allowed to be inserted is called the team end.
For example, queuing for shopping. Job queuing in the operating system. Members entering the queue first always leave the queue.
When no element exists in the queue, it is called an empty queue. Add the a1, a2,… elements to the empty queue in sequence ,..., After an, a1 is the first element and an is the end element. Obviously, the order of exiting the queue can only be a1, a2 ,..., An, that is, the queue is modified according to the first-in-first-out principle.
Definition of abstract data types of queues
ADT Queue {Data Object: D = {ai | ai ε ElemSet, I = 1, 2 ,..., N, n> = 0} Data Relationship: R = {<ai-1, ai> | ai-1, ai, D, I = 2, 3 ,..., N} indicates that the a1 end is the first and the an end is the end of the team. Basic operation: Create (): Create an empty queue; EmptyQue (): If the queue is empty, true is returned; otherwise, flase; queue metadata InsertQue (x) is returned ): insert element x to the end of the team; DeleteQue (x): Delete the first element x;} ADT Queue
Static ordered storage structure of queues
In a non-empty queue, the first pointer always points to the first element, while the last pointer always points to the next position of the last element.
There is a "false overflow" in the ordered queue. The reason is that the header and tail pointers only increase or decrease in the number of inbound and outbound operations, so that the space of the deleted elements will never be reused. Therefore, although the actual number of elements in the queue may be much smaller than the array size, the tail pointer may exceed the upper bound of the Vector Space and cannot be entered into the queue. This phenomenon is called false overflow.
Cyclic queue
Cyclic Queue: The vector space allocated to the Queue is regarded as a ring that is connected at the beginning and end, and this Queue is called a Circular Queue ).
Features:
Overcome the above "false overflow" phenomenon and make full use of Vector Space
When the team leaves and enters the team, the first and last pointer of the team must be added to 1 and move forward.
When the first and tail Pointer Points to the upper bound of the vector (MAX_QUEUE_SIZE-1), the result of the 1 operation is to point to the lower bound of the Vector 0.
2. Status Insert_CirQueue (SqQueue Q, ElemType e)/* Insert the Data Element e to the end of the Q queue */{if (Q. rear + 1) % MAX_QUEUE_SIZE = Q. front) return ERROR;/* if the team is full, the ERROR mark */Q is returned. queue_array [Q. rear] = e;/* element e enters the queue */Q. rear = (Q. rear + 1) % MAX_QUEUE_SIZE;/* move the team tail Pointer Forward */return OK;/* the team is successfully added */}
2. Queue entry (correct) Status Insert_CirQueue (SqQueue * Q, ElemType e) /* Insert the Data Element e to the end of Q in the cyclic queue */{if (Q-> rear + 1) % MAX_QUEUE_SIZE = Q-> front) return ERROR; /* full team, error flag returned */Q-> Queue_array [Q-> rear] = e; /* element e enters the queue */Q-> rear = (Q-> rear + 1) % MAX_QUEUE_SIZE;/* move forward the tail pointer of the Team */return OK; /* successfully joined the team */}
3. The exit Operation Status Delete_CirQueue (SqQueue Q, ElemType * x)/* leaves the first element of Q in the cyclic queue */{if (Q. front = Q. rear) return ERROR;/* empty team, return ERROR flag */* x = Q. queue_array [Q. front];/* Get the first element */Q. front = (Q. front + 1) % MAX_QUEUE_SIZE;/* move the first pointer of the Team forward */return OK ;}
3 outbound operations (correct) Status Delete_CirQueue (SqQueue * Q, ElemType * x) /* leave the first element of Q in the cyclic queue */{if (Q-> front = Q-> rear) return ERROR;/* empty team, return Error flag */* x = Q-> Queue_array [Q-> front];/* element of the first line */Q-> front = (Q-> front + 1) % MAX_QUEUE_SIZE;/* move the first pointer of the Team forward */return OK ;}