Computer Science · Ch 4 — Queue
First In First Out (FIFO)
First In First Out (FIFO)
A queue is built on a simple, real-world rule: the first person to join the line is the first person to be served. This principle is called First In First Out, or FIFO.
In a FIFO queue, the element that has been waiting the longest is always the one that gets removed first. This is the same logic as a line at a ticket counter or a queue at a bus stop — the first person to arrive is the first to leave. Because of this, the FIFO approach is also known as First Come First Served (FCFS).
The queue has two specific ends that define how it works:
- REAR (also called TAIL): This is the end where new items are added. When you join the queue, you always go to the rear.
- FRONT (also called HEAD): This is the end from which items are removed. When it is your turn, you leave from the front.
The terms REAR/TAIL and FRONT/HEAD are used interchangeably in textbooks and exam questions. Remember that addition happens at one end and removal happens at the opposite end — never in the middle.
To summarise the key features of a FIFO queue:
- It follows the FIFO (First In First Out) principle. …