4.1.1.4 Queues (FIFO, Priority Queues)
4.1.1.4 Queues: First In, First Out (FIFO)
Now, imagine a line of people waiting for a roller coaster. The first person who gets in line is the first person to get on the ride. A queue in computer science is just like that!
It follows a rule called FIFO, which stands for First In, First Out. The first item you put in the queue is always the first item to come out.
- Adding items to a queue is called "enqueuing." (Like joining the end of the line)
- Removing items from a queue is called "dequeuing." (Like the person at the front of the line getting on the ride)
- Examples of Queues in real life:
- A printer queue: The first document sent to the printer is the first one that gets printed.
- Customer service lines: The first caller in line is the first one to be helped.
Priority Queues
Sometimes, in a line, certain people get to go first, even if they weren't first in line. Think of an emergency room at a hospital: the most urgent cases are seen first, regardless of when they arrived. This is like a priority queue.
In a priority queue, each item has a "priority" level. Items with higher priority get to come out of the queue before items with lower priority, even if the lower-priority items arrived first. If two items have the same priority, then the FIFO rule (first in, first out) usually applies to them.
- Example: A hospital emergency room, or tasks in a computer's operating system where some tasks are more important than others.
Bibliography for Queues
- Kiddle: Data structure Facts for Kids
- High Speed Training: Using a FIFO Food Storage System | Guidance & Free Checklist
- State Food Safety: April Cartoon: First In, First Out (FIFO)
- LMU Computer Science: Priority Queues
- GeeksforGeeks: Introduction to Priority Queue