queue (linear table)

queue

 

(Linear table)


        A queue is a special kind of linear table, which is a first-in, first-out (FIFO) data structure. It only allows delete operations on the front of the table and insert operations on the rear of the table. The end that performs the insert operation is called the tail of the queue, and the end that performs the delete operation is called the head of the queue. When there are no elements in the queue, it is called an empty queue.


Introduction

 In the data structure of the queue, the first element inserted will be the first to be deleted; otherwise, the last element inserted will be deleted last, so the queue is also called "first in, first out" (FIFO—first in first out) linear surface.

Queue empty condition: front=rear
Queue full condition: rear = MAXSIZE
The queue can be stored in an array Q[1...m], and the upper bound m of the array is the maximum capacity allowed by the queue. In the operation of the queue, two pointers need to be set : head, the head pointer, pointing to the previous position of the actual head element; tail, the tail pointer, pointing to the position where the actual tail element is located. Under normal circumstances, the initial value of the two pointers is set to 0, then the queue is empty and there are no elements. Figure 1(a) shows a queue consisting of 6 elements, and the array defines Q[1…10]. Q(i) i=3,4,5,6,7,8 head pointer head=2, tail pointer tail=8. The number of elements in the queue is: L=tail-head Now to dequeue the element at the head of the queue, the head pointer needs to be incremented by 1. That is, when head=head+1, the head pointer moves up one position and points to Q(3), indicating that Q(3) has been dequeued. See Figure 1(b). If you want to enqueue a new element, you need to move the tail pointer up one position. That is, when tail=tail+1, Q(9) joins the queue. When the tail of the queue has been processed at the top, that is, tail=10, if the enqueue operation is to be performed, an "overflow" will occur, but there are actually three empty positions in the queue, so this overflow is called " false " overflow ".
There are two ways to overcome false overflows. One is to move all the elements in the queue to the lower address area, which is obviously a waste of time; the other is to treat the array storage area as an end-to-end annular area. When stored at address n, the next address is "flipped" to 1. Queues constructed using this technique for storage are called circular queues .
Queues, like stacks, only allow insertion and removal of elements at endpoints (front or back).
The enqueue algorithm of the round robin team is as follows:
1、tail=tail+1;
2. If tail=n+1, then tail=1;
3. If the head=tail tail pointer coincides with the head pointer, it means that the element has filled the queue, and an overflow error will be handled;
4. Otherwise, Q(tail)=X, end (X is the new incoming and outgoing element).
Queues, like stacks, have a very wide range of applications.

(1) Initialize the queue Qini (Q)

(2) Join QADD(Q,X)
(3) Dequeue QDel(Q,X)
(4) Determine whether the queue is empty qempty(Q)
(5) Determine whether the queue is full qfull(Q)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324769627&siteId=291194637