Skip to content

Computer Science · Ch 4 — Queue

Introduction to Queue

4.1

Introduction to Queue

The Idea of a Queue

A queue is a data structure that follows the First-In-First-Out (FIFO) principle. This is the opposite of a stack, which works on Last-In-First-Out (LIFO). In a queue, the element that is added first is the one that gets removed first. Think of it as a line of people waiting for something — the person who arrives first is served first.

A queue is an ordered linear list of elements. The key feature that distinguishes it from a stack is that it has two different ends: one end is used only for adding elements, and the other end is used only for removing elements. This is unlike a stack, where both adding and removing happen at the same end.

Everyday Examples of a Queue

The textbook gives three clear, real-world examples to make the FIFO concept intuitive:

  • Students standing in a queue for morning assembly — the first student to join the line is the first to enter the assembly ground.
  • Customers forming a queue at a bank cash counter — the person at the front of the line is served by the next available cashier.
  • Vehicles queued at fuel pumps — the first car to arrive at the pump gets filled up first.

In all these cases, the order of arrival determines the order of service. This is exactly how a queue data structure works in computer science.

Figure 4.1Queue of people at a bank
Fig. 4.1 — Queue of people at a bank

Drawn by us to help you understand the concept clearly, and verified to make sure it's accurate. For exams, practice from your textbook's own diagram.

A queue at a bank counter is the real-life picture behind the Queue data structure. Customers stand in a single line facing the cashier — the person at the FRONT of the line is served next (the cashier's "Next" call), while any new arrival can only join at the REAR, behind everyone already waiting.

No one can jump ahead of someone who arrived earlier, and no one is served out of turn — the order you join is the order you get served. That's exactly the rule a Queue enforces: First In, First Out (FIFO).