Concepts / Queue Operations: Enqueue and Dequeue

Queue Operations: Enqueue and Dequeue

Queues are used when only one item can be processed at a time but several items may be waiting for processing.

  • Programming
Interactive lab

Try it: Stack and Queue

The difference between a stack (last in, first out) and a queue (first in, first out).

How it works

  1. A stack adds (push) and removes (pop) at the same end, the top — the last item added leaves first.
  2. A queue adds (enqueue) at the back and removes (dequeue) from the front — the first item added leaves first.
  3. peek / front look at the next item without removing it.
  4. Adding to a full structure is an overflow; removing from an empty one is an underflow.

Default run (3 steps): Stack (bottom → top): 4, 7. 2 of 8 slots used. Choose an operation. Example: push 9. … Stack is now: 4, 7, 9. Size 3/8.

Simplified: Fixed capacity of 8 items so overflow can be shown; Python lists and collections.deque grow as needed.

Educational simulation

Loading the simulation…

A Line That Keeps Order

Imagine a single service counter that can serve only one customer at a time, with a line of people forming behind it. Nobody is served out of turn, and newcomers join the end of the line. Computer systems face this same situation constantly. A processor, a program, or a server may be able to handle only one item at a time, while other items keep arriving and must wait. A queue is the structure that holds those waiting items in the order they arrived.

The source describes the situation directly: queues are typically used in a computer, just as in real life, when only one item can be processed at a time but several items can be waiting for processing. As you read this article, keep one question in mind for every example: what is waiting, and who takes the next item off the front?

Watching Items Enter and Leave

Two operations do all the work in a queue. Enqueue adds an item at the back, the end where new arrivals join. Dequeue removes the item at the front, the item that has waited longest. Items are never taken from the middle, and new items never jump ahead of ones already waiting.

What do you think happens?

Items A, B, and C are enqueued in that order. Then one item is dequeued. Which item leaves the queue, and what remains?

  • A leaves; B and C remain

  • C leaves; A and B remain

  • B leaves; A and C remain

Reveal answer
Answer:

A leaves; B and C remain

A joined first, so A is at the front. Dequeue always removes the front item, so A is removed. B moves to the front and C stays behind it.

The diagram below traces the same sequence one operation at a time. Each step shows the queue's contents after the operation is finished. Notice that the dequeue step changes only the front of the queue; the items behind it keep their places.

Enqueue Aqueue: AEnqueue Bqueue: A, BEnqueue Cqueue: A, B, CDequeueremoves A; queue: B, Cnext operationnext operationnext operation
After A, B, and C are enqueued and one item is dequeued, which item leaves, and how do the contents change at each step?

Why Order Is Preserved

Definition

A queue is a waiting structure in which items are removed in the same order they were added. The item that arrived first is the first one to be processed. This is often described as first in, first out.

The order is preserved because of where items enter and leave. Arrivals always go to the back, and the item removed is always the one at the front. An item that arrived earlier is therefore always nearer the front than any item that arrived later. Nothing can overtake it while it waits.

It helps to track two positions: the front, where the next item will be removed, and the rear, where the most recent arrival sits. Each enqueue moves the rear to the new item. Each dequeue moves the front to the next waiting item. The diagram below shows the same three waiting items with their positions labeled.

Position 1A (front)Position 2BPosition 3C (rear)waits behindwaits behind
Why does the item enqueued first always reach the front, and how do the front and rear move as items are added and removed?

Where Queues Appear in Computing

The same pattern appears in three places the source describes. For each one, ask what is waiting and who takes the next item off the front.

Tracing a Busy Server

Worked example

Three requests arrive while the server is busy

A server is working on a request when requests R1, R2, and R3 arrive in that order. Once it is free, in what order are the requests processed, and why?

  1. 1
    Identify the waiting items

    The server is busy, so R1, R2, and R3 are each placed into the queue as they arrive. The queue now holds R1 at the front, then R2, then R3 at the rear.

  2. 2
    Apply the rule for the server

    When the server is free, it takes work from the front of the queue. The front item is the one that arrived earliest among those still waiting.

  3. 3
    Trace the removals

    The server takes R1 first. R2 then moves to the front, and it is taken next. R3 is processed last. Each removal leaves the remaining requests in their original arrival order.

Result

The requests are processed in the order R1, R2, R3, the order in which they were received. No request is skipped, and R3 does not overtake the earlier requests.

The same reasoning applies to the thread queue and the event queue. In each case the next item processed is the one at the front, and every new arrival is placed at the back. Once you can name what is waiting and what sits at the front, you can predict the processing order of any queue.

Mistakes Beginners Make with Queues

  • Common mistake

    Assuming the most recent arrival is processed first

    What it looks like

    Items A, B, and C are enqueued in that order, and the learner expects C to be dequeued first.

    Why it fails

    A queue removes from the front, and the front holds the item that has waited longest, which is A.

    Do this instead

    Dequeue removes the earliest arrival. The newest item waits at the rear.

  • Common mistake

    Thinking a thread leaves the queue after its first turn

    What it looks like

    A thread receives processing time and the learner assumes it is finished and gone.

    Why it fails

    The source states that a thread that has not terminated returns to the back of the queue.

    Do this instead

    A thread keeps circling back until it terminates.

  • Common mistake

    Expecting the queue to reorder waiting items by importance

    Why it fails

    The source's guarantee is that items are processed in the order they arrived, such as keystrokes in the order typed or requests in the order received.

    Do this instead

    Treat the queue as strictly arrival-ordered unless a different structure is used.

  • Common mistake

    Confusing a queue with a stack, where the most recently added item is removed first

Practice: Predicting the Processing Order

MEDIUM

A single worker processes items from a queue. Items W, X, Y, and Z are enqueued in that order. The worker dequeues one item, then two more items are enqueued as Q and R. Write the order in which the worker will process all six items, and explain the reason for each position.

Hints
  • Start by writing the queue contents after each operation.
  • Q and R join the rear, so they wait behind Z.
  • The worker always takes from the front.

Key Points to Carry Forward

  1. 1
    A queue holds waiting items when only one item can be processed at a time.
  2. 2
    Enqueue adds an item at the back; dequeue removes the item at the front.
  3. 3
    Items are processed in the order they arrived, because arrivals join the back and the front is always the earliest waiting item.
  4. 4
    Threads, the event queue, and web server requests all follow this same pattern, and the question to ask is always what is waiting and who takes the next item.

Key Takeaways

  • A queue holds items that must wait when only one item can be processed at a time.
  • Enqueue adds at the back and dequeue removes from the front, so the earliest arrival is processed first.
  • Relative order of waiting items is preserved as items are added and removed.
  • Thread management, the event queue, and web server requests all use this same arrival-ordered pattern.