Skip to content

Queue


Task: Add three values to the queue

frontrear12275FRONT OF THE QUEUE12arrived first

Why we need a Queue

Analogy

  • Imagine you are standing in a line at a ticket counter. Riya comes first. Then Arjun. Then Sara.
  • Riya gets her ticket first and leaves. Arjun is next. Sara waits behind Arjun.
  • A person who joins later cannot leave before someone who was already waiting.
  • That is the basic idea of a queue: first in, first out.
DEQUEUEleaves at frontENQUEUEjoins at rearRiyaArjunSaraFRONTREAR

What is a queue?

Definition

A queue is a data structure that follows the FIFO rule: First In, First Out.

There are two ends. The front is where elements leave, and the rear is where new elements join.

So enqueue goes to the rear, and dequeue takes from the front.


The four basic operations

OperationWhat it does
enqueue(x)Adds x at the rear
dequeue()Removes the element from the front
peek()Shows the front element
isEmpty()Checks if the queue is empty
enqueue(10)10enqueue(20)1020enqueue(30)102030dequeue()203010RETURNS 10

Try it yourself

First in, first out

outin12front275rearempty
Add a value to begin
3 waiting

Try dequeue a few times. The front leaves and everyone behind moves up a place, which is the one thing a queue does that a stack never has to.


Queue in one picture

FRONTREAR10203040LEAVEENTER

New elements join at the rear. Old elements leave from the front.

That is why the first element added is the first one removed.


A queue using an array

A queue can be stored inside an array.

But a normal array creates a small problem. Suppose the queue reaches the end of the array and some elements at the beginning have already been removed. Those empty spaces are still free, but the rear has reached the end.

FRONTREARemptyi = 0emptyi = 1emptyi = 2emptyi = 310i = 420i = 5four free slots the rear cannot reach

We do not want to waste that space.


Circular queue

The solution is a circular queue. Instead of stopping at the last index, the queue goes back to the beginning.

REARFRONT30i = 040i = 1emptyi = 2emptyi = 310i = 420i = 5after index 5, the rear wraps back to index 0

So after index 5, the next position is index 0. This lets us reuse the empty spaces at the beginning.

Note

When the rear reaches the end, it wraps around to the beginning. That is why it is called a circular queue.


Queue vs stack

The easiest way to remember the difference:

  • Stack: the last person leaves first.
  • Queue: the first person leaves first.
QUEUE10203040INOUT (10)STACK10203040INOUT (40)
QueueStack
RuleFIFOLIFO
ExampleLine of peoplePile of plates
Addenqueuepush
Removedequeuepop
Main endFront and rearTop
Common useScheduling, BFSUndo, DFS

Ask yourself: do I need the newest item first? That is a stack. Do I need the oldest item first? That is a queue.


Where are queues used?

Queues are useful whenever things need to be handled one after another in order.

  • Printer jobs: print jobs run in the order they arrive
  • CPU scheduling: processes wait for their turn
  • BFS: visits nodes level by level
  • Customer service: customers are served in order
  • Network requests: requests wait in a queue
  • Message systems: messages wait to be processed

The common idea is simple: something arrives, waits, then gets processed.


Queue in code

You do not need much code to understand a queue. In Python, deque can be used:

Python
from collections import deque queue = deque() queue.append(10)   # enqueuequeue.append(20) queue.popleft()    # dequeue -> 10queue[0]           # peek -> 20

After adding 10 and 20, the queue holds 10, 20 with 10 at the front. The first item added is the first one removed.


Common mistakes

  • Adding at the front. A normal queue adds new elements at the rear.
  • Removing from the rear. A normal queue removes elements from the front.
  • Forgetting the empty case. You cannot dequeue when the queue is empty.

Watch out

In Python, list.pop(0) works, but removing the first element from a normal list takes O(n) time.

Use deque when you need an efficient queue.


Quick recap

A queue is a line of people. FIFO, first in first out.

  • Enqueue: add at the rear
  • Dequeue: remove from the front
  • Peek: see the front element
  • isEmpty: check whether the queue is empty
  • Circular queue: reuse space by wrapping around

Key takeaway

New things join at the back. Old things leave from the front.