1. Queuing: The Early Form of Queues in Daily Life

In daily life, we encounter “queuing” every day: queuing to get food in the cafeteria, queuing to buy milk tea, and even queuing for medical registration. All these queuing scenarios share a common rule: the first to arrive is served first. For example, if you are the 3rd in line at a milk tea shop, you will be served only after the people in front of you have finished ordering. The last person to arrive has to wait until everyone else has left. This “first-come, first-served” rule is actually the core idea of the “queue” data structure in the field of computer science.

2. What is a Queue?

A queue (Queue) is a data structure with “First-In-First-Out” (FIFO) behavior. Imagine a line at a store; it has two key operations:
- Enqueue: A new element (such as a new customer) is added to the end of the line (rear).
- Dequeue: The element that was added earliest (front) is removed, and the next element gets its turn.

In addition to these two core operations, a queue also has operations like “peek” (to see who is at the front of the line) and “check if the queue is empty”.

3. Difference Between Queue and Stack (Simple Comparison)

To better understand queues, we can compare them with another common data structure: the “stack”.
- Stack: Follows “Last-In-First-Out” (LIFO), like putting books into a jar— the last book you put in is the first you take out (e.g., the “undo” operation).
- Queue: Follows “First-In-First-Out” (FIFO), like a line— the earliest arrival is served first.

A simple way to remember: A stack is “last to come, first to go”, while a queue is “first to come, first to go”.

4. Typical Applications of Queues in Daily Life

Queues are not just abstract mathematical concepts; they are ubiquitous in computer science and daily life, solving numerous problems that require “sequential processing”.

1. Task Scheduling: How Does a Computer Handle Multiple Tasks?

Have you ever wondered why your computer doesn’t “freeze” when you have multiple programs open (e.g., browser, WeChat, Word)? In reality, computers manage tasks using a queue, just like a cafeteria line:
- Enqueue Task: A new task (e.g., opening WeChat) is added to the end of the “task queue”.
- Dequeue Task: The CPU, like a cafeteria staff, takes tasks from the front of the queue in order. The earlier a task is enqueued, the earlier it is processed (e.g., the browser you opened first gets more CPU time).

Example: When charging your phone, after plugging in the charger, the charging task is processed first, followed by other background tasks. This is also a queue application—ensuring each task is executed in the order it arrived.

2. Breadth-First Search (BFS): Finding the Shortest Path in a Maze

In programming, we often encounter problems like “finding the shortest path” (e.g., the minimum number of steps to get from the start to the end in a maze). Here, a queue is the key tool:
- Enqueue the Start Point: Mark the start point of the maze as “visited” and add it to the queue.
- Expand Layer by Layer: Each time you dequeue a position, add its adjacent positions (e.g., up, down, left, right) to the queue if they haven’t been visited.
- Dequeue First to Reach the End: Since the queue follows FIFO, the first time you reach the end is the shortest path (because each step is processed in increasing order of distance from the start).

Metaphor: This is like a teacher asking students to find the classroom. The first student (front of the queue) will direct others, ensuring the shortest time to find the target.

3. Buffering and Rate Limiting: A Queuing Mechanism to Prevent System “Crash”

During e-commerce promotions (e.g., “Double 11”), when a large number of users simultaneously try to purchase items, the system might “crash”. Here, a queue acts as a “buffer”:
- Enqueue Requests: User clicks on “Buy Now”, and the request is first added to the “request queue” instead of directly hitting the server.
- Process Requests: The server processes requests one by one in queue order (e.g., processing 100 requests per second), avoiding overload from handling all requests at once.
- Rate Limiting Protection: If the queue becomes too long (e.g., exceeding 1000 requests), the system will show “Please wait in line”, preventing resource exhaustion.

Example: Like when there are too many people at a concert, a “queuing buffer zone” is set up to allow entry in batches, avoiding congestion at the entrance.

4. Multithreading Collaboration: A “Conveyor Belt” for Producers and Consumers

In programming, we often need to coordinate “producers” (e.g., threads writing data) and “consumers” (e.g., threads reading data). A queue can act as a “conveyor belt” between them:
- Producer Enqueue: Producers continuously add data to the queue (e.g., generating order information).
- Consumer Dequeue: Consumers retrieve data from the queue (e.g., processing orders).

Benefit: Producers and consumers can work asynchronously. The queue ensures data is not lost and avoids errors due to speed mismatches (e.g., if producers are faster than consumers, the queue will buffer the data).

5. Why Learn About Queues?

The core value of a queue is “processing in order”. In programming or algorithms, you should use a queue when:
- You need to handle a sequence of data arriving in order (e.g., message pushes, log recording).
- You need to implement “first-come, first-served” logic (e.g., printer tasks, task scheduling).
- You need to avoid resource conflicts (e.g., when multiple threads share data, use a queue for buffering).

Beginner’s Tip: When you encounter a “queuing processing” requirement, prioritize using a queue! For example, LeetCode problems like “Implement Stack Using Queues” and “Binary Tree Level-Order Traversal” are essentially queue applications.

6. Conclusion

A queue is like the “rule of queuing” in daily life, with the core principle of “first-come, first-served”. From task scheduling to system buffering, from algorithmic search to multithreading collaboration, queues are everywhere. The key to understanding a queue is remembering its “FIFO” principle—this is not only a fundamental data structure but also a practical tool for solving real-world problems.

Next time you are in a queue, think to yourself: “Could this scenario be solved with a queue?” You might suddenly realize that programming and daily life are interconnected!

Xiaoye