This is the 37th day of my participation in the First Wen Challenge 2022.First challenge in 2022.
Data structure queue
Queues are also interesting in dealing with temporary data, which, like stacks, are constrained arrays. The difference lies in the order in which we want to process the data, which of course depends on the application scenario.
You can think of a queue like a line at a movie theater. The first person in line will be the first to leave and enter the theater. When applied to queues, the first person to join the queue will be removed from the queue first. So computer scientists use the acronym “FIFO” (first in, first out) to describe it.
Like stacks, queues have three limits (but different contents).
- Data can only be inserted at the end (like a stack).
- Only the first data can be read (as opposed to the stack).
- Only the initial data can be removed (this is also the opposite of the stack).
Now to see how it works, prepare an empty queue.
First, insert 5 (although the insertion of a stack is called pushing the stack, there is no fixed name for the insertion of a queue, which can be called put, join, or join).
And then we insert 9.
And then I insert 100.
So far, the queue behaves like the stack, but if you remove data, it will be the opposite of the stack, because the queue removes data from the beginning.
If you want to remove data, you have to start at 5, because that’s where it started.
And then we remove the 9.
So you’re down to 100.
Queues are widely used, ranging from printer job Settings to background tasks of network applications.
Queues are also an ideal tool for handling asynchronous requests — they ensure that requests are executed in the order they are received. In addition, it is often used to simulate real-world situations where things need to be done in an orderly manner, such as planes lining up to take off or patients lining up to see a doctor.
conclusion
As you can see, stacks and queues are clever programming tools for solving real-world problems.
Knowing stacks and queues unlocks the next goal: learning stack-based recursion. Recursion is also the basis for other advanced algorithms