Data structure and algorithm - what is a queue (queue storage structure)

A queue, like a stack, is also a linear storage structure that has strict requirements on data "storage" and "retrieval".

Different from the stack structure, both ends of the queue are "open", requiring data to only enter from one end and exit from the other end , as shown in the following figure:

queue storage structure

Usually, the end of the data entry is called the "team tail", and the end of the data output is called the "queue head". The process of data elements entering the queue is called "queue entry", and the process of leaving the queue is called "queue exit".

Not only that, the entry and exit of data in the queue must follow the "first in, first out" principle , that is, the data elements of the most advanced queue must also be out of the queue first. Taking the queue in the above figure as an example, it can be analyzed from the storage status of data in the queue that element 1 is the most advanced, followed by element 2, and finally element 3. At this time, if element 3 is dequeued, according to the "first in, first out" characteristic of the queue, element 1 must be dequeued first, element 2 will be dequeued, and element 3 will be out of the queue at last.

Don't confuse the stack with the queue. The stack structure is closed at one end, which is characterized by "first-in, last-out"; while both ends of the queue are open, and the characteristic is "first-in, first-out".

Therefore, data enters from one end of the table and exits from the other end, and a linear storage structure that follows the "first in, first out" principle is a queue.

There are two ways to implement the queue storage structure:

  1. Sequence queue: a queue structure implemented on the basis of a sequence table;
  2. Chain queue: a queue structure implemented on the basis of a linked list;

The difference between the two is only the difference between the sequential list and the linked list, that is, in the actual physical space, the queue stored in the data center is a sequential queue, and the queue stored in a decentralized manner is a chain queue.

In real life, the application of queues can be seen everywhere, such as queuing up to buy XXX, the registration system of hospitals, etc., all adopt the structure of queues.

Take queuing to buy tickets as an example, all people line up in a line, those who come first will go to the front, and those who come later can only wait in line from the end of the line, and everyone in the line must wait until everyone in front of them has bought all of them. After the ticket is successfully issued and the team is discharged from the head of the team, it is your turn to buy the ticket. Isn't this a typical queue structure?

After understanding what a queue is, you can start to systematically study sequential queues and chain queues.

2023 new version of data structure and algorithm Java video tutorial (Part 1), data structure and algorithm that senior java programmers must learn
2023 new version of data structure and algorithm Java video tutorial (part 2), data structure and algorithm that java senior programmer must learn

Supongo que te gusta

Origin blog.csdn.net/Itmastergo/article/details/131910531
Recomendado
Clasificación