Introduction to Queue

Picture this. You go to a movie theater to buy tickets. There is a line in front of the counter. The person who came first stands at the front and gets served first. You came last. So you stand at the back and wait your turn. Nobody jumps the line. That simple idea is exactly what a queue is in programming.

A queue is a way to store items where the first item you add is the first item you take out. We call this rule FIFO. It means First-In-First-Out. So the order is fair. Whoever came first leaves first.

🎬 A Real-World Analogy

Think about that ticket counter line one more time.

  • New people join at the rear of the line. The rear is the back.
  • The person at the front gets the ticket and leaves.
  • Service happens in the same order people arrived.

So the line only grows at the back. And it only shrinks at the front. A queue in code works the exact same way. You add at one end and remove from the other end.

Note

A stack is the opposite. A stack is Last-In-First-Out, like a pile of plates where you take the top one first. A queue is First-In-First-Out, like a line of people. Keep these two pictures separate in your head.

πŸ€” Why Do We Need a Queue?

Here is the pain. Sometimes you have a bunch of jobs waiting. And you must handle them in the order they arrived. If you grab a random job, things become unfair and messy. A queue solves this. It keeps the arrival order for you. So the oldest waiting job is always the next one you handle.

You see queues all around you in real software.

  • Printer jobs line up. Ten people send documents to one printer. The first document sent is the first one printed.
  • The operating system schedules tasks. It lines them up and runs them in turn.
  • BFS uses a queue. BFS means Breadth-First Search. It is a way to search a graph or tree by exploring neighbors level by level. The queue remembers which node to visit next.
  • Customer support uses a queue too. Messages get answered in the order they came in.

πŸ”§ The Four Core Operations

A queue is simple. There are only a few things you ever do with it. Here is what each one means.

  • enqueue β€” add a new item at the rear (the back). This is like a new person joining the line.
  • dequeue β€” remove the item at the front and return it. This is like the front person getting served and leaving.
  • front β€” look at the item at the front without removing it. Just peek. Don’t take.
  • isEmpty β€” check if the queue has no items left. It returns true or false.

The word enqueue just means β€œput into the queue”. And dequeue means β€œtake out of the queue”. Don’t let the names scare you. They are only fancy words for add and remove.

Tip

Remember the two ends. You always add at the rear and remove from the front. They never swap. If you ever add and remove from the same end, that is not a queue anymore.

πŸ“Š How It Looks Step by Step

Let’s say we enqueue three numbers: 10, then 20, then 30. The front is on the left and the rear is on the right.

Front

10

20

30

Rear

Now we call dequeue once. The front item 10 leaves first because it came first. What stays is 20 and 30. Now 20 is the new front.

Front

20

30

Rear

See how the line shrinks from the front and grows from the rear? That is the whole idea of FIFO in one picture.

Caution

Be careful when the queue is empty. If you try to dequeue from an empty queue, there is nothing to remove. This is called underflow. Always check isEmpty first before you remove.

πŸ’» A Tiny Queue Demo in Code

Let’s make a small queue. We add a few items. Then we remove them one by one. Most languages already give us a built-in tool for this. So we don’t have to build the queue from scratch yet. We just use it and watch the FIFO order in action.

Here we enqueue 10, 20, and 30. Then we peek at the front, dequeue twice, and check what is left.

queue_demo.py
from collections import deque
# deque is a fast built-in we can use as a queue
queue = deque()
# enqueue adds at the rear
queue.append(10)
queue.append(20)
queue.append(30)
print("Front item:", queue[0]) # peek at front
print("Removed:", queue.popleft()) # 10 leaves first
print("Removed:", queue.popleft()) # then 20
print("New front:", queue[0]) # 30 is left
print("Is empty?", "yes" if not queue else "no")

The output of the above code will be:

Front item: 10
Removed: 10
Removed: 20
New front: 30
Is empty? no

Look at the output. The number 10 went in first and came out first. Then 20. That is FIFO working exactly like the ticket line.

⏱️ How Fast Are These Operations?

Good news. The main queue operations are very fast. They do not depend on how many items are inside. So each one runs in O(1) time. Here is a quick look at the time cost.

Operation What It Does Time
enqueue Add an item at the rear O(1)
dequeue Remove the front item O(1)
front Peek at the front item O(1)
isEmpty Check if the queue is empty O(1)

Note

O(1) means the work stays the same no matter how big the queue gets. Adding to a queue with 5 items takes the same time as adding to a queue with 5 million items. That is why queues are so handy for fast, fair processing.

⚠️ Common Mistakes to Avoid

A few small slip-ups trip up beginners. Watch out for these.

  • Removing from the wrong end. You always remove from the front, never from the rear.
  • Forgetting to check isEmpty before dequeue. Removing from an empty queue causes an error called underflow.
  • Mixing up queue and stack. Queue is FIFO, like a line of people. Stack is LIFO, like a pile of plates.
  • In JavaScript, using shift() on a huge array can be slow. It moves every element each time. For real heavy work, a proper queue structure is better.

🧩 What You’ve Learned

βœ… A queue is a FIFO structure. The first item added is the first one removed, just like a line at a ticket counter.

βœ… You add items at the rear with enqueue and remove from the front with dequeue.

βœ… The front operation lets you peek without removing. The isEmpty operation tells you if the queue has any items.

βœ… Queues power real things like printer jobs, task scheduling, and BFS in graphs and trees.

βœ… The core operations enqueue, dequeue, front, and isEmpty all run in O(1) time.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    What does FIFO stand for in a queue?

    Why: A queue follows First-In-First-Out, so the first item added is the first one removed.

  2. 2

    Which operation adds an item to a queue, and at which end?

    Why: enqueue adds a new item at the rear (the back) of the queue.

  3. 3

    You enqueue 5, then 8, then 2. What does the first dequeue return?

    Why: 5 was added first, so by the FIFO rule it is the first one removed.

  4. 4

    Why should you check isEmpty before calling dequeue?

    Why: Removing from an empty queue has nothing to remove and causes an underflow error.

πŸš€ What’s Next?

Now you know what a queue is and how FIFO works. Next, go deeper into each operation. Then build a queue yourself.

Share & Connect