This is the 12th day of my participation in the August More Text Challenge. For details, see:August is more challenging

If you want to keep writing, write a series. What can I write about? Program = algorithm + data structure, see the importance of algorithm and data structure. This series of old poems tries to explain algorithms and data structures in the simplest possible language, from the shallow to the deep. If you are interested, check out the column.

All the previous videos have been about algorithms, but today I’m going to show you a data structure.

Data structures are stored and read and write according to special data storage methods. They all have their own rules. Today I’m going to talk to you about queues.

A description of the queue

A Queue, “Queue,” is a linear storage structure that is “first in, first out.”

The head of the queue can only output, and the tail can only input

(1) Head and tail: The part that can be output is called head, and the part that can be input is called tail

(2) Line up: Put the data at the end of the line

(3) Out of the team: take the data out of the team leader

In fact, the queue data structure is relatively easy to explain. It is the same as when we go to the canteen to queue for food, one after the other, the end of the meal is called the head of the line, and then the new person inserted from the end of the line. It’s as simple as that.

#include <queue> #include <iostream> using namespace std; int main(){ queue<int> q; for (int i = 0; i < 10; i++){ q.push(i); } if (! Q.empty ()){cout << "queue q is not empty!" << endl; Cout << "q "with" << q.size() << "elements" << endl; } cout << "the team header element is:" << q.front() << endl; Cout << "q.ack () << endl; for (int j = 0; j < 10; j++){ int tmp = q.front(); cout << tmp << " "; q.pop(); } cout << endl; if (! Q.empty ()){cout << "Queue is not empty!" << endl; } system("pause"); return 0; }Copy the code

The above example is c++. In c++, other people already encapsulate queues. We can import the #include

header file and use it directly.

The above operation is to queue 10 numbers from zero, then call empty, and then one person out of the queue.

The order of entry is 0123456789, and the order of exit remains the same.

More dry content, project source code, you can pay attention to the public number: like the code poem

Now that I’m in, it’s not easy to be original. Let’s go with a “like”.