What is a queue?

The key idea

A queue processes items in first-in-first-out order, or FIFO. An item joins at the back, and the earliest remaining item is taken from the front. This fits a task whose rule is to preserve arrival order.

FIFO is not a claim that every real service should follow the same rule. A system with priorities, appointments or different eligibility rules may need a different design.

Trace a fictional print queue

EventWaiting jobs from front to backNext job
A arrivesAA
B arrivesA, BA
A is removed for processingBB
C arrivesB, CB
B is removedCC

Removing a job from the waiting queue does not prove the printing succeeded. A real application may also track processing, completion and failure. Keep those lifecycle states separate from the ordering structure.

Try the C++ operations

#include <iostream>
#include <queue>
#include <string>

int main() {
    std::queue<std::string> jobs;
    jobs.push("job-a");
    jobs.push("job-b");
    while (!jobs.empty()) {
        std::cout << jobs.front() << '\n';
        jobs.pop();
    }
}

The output is job-a followed by job-b. The code checks emptiness before inspecting front. Like std::stack, std::queue has a pop operation that removes an item without returning it.

Read the front value before removing it when the caller needs the item. Do not confuse back, which inspects the most recently added item, with the item that should leave next under FIFO.

Capacity needs its own policy

A bounded queue has a maximum capacity. When it is full, the application needs an explicit choice: reject a new item, wait under controlled rules, or use another documented policy.

Silently overwriting a waiting item changes the meaning of the queue. In a classroom exercise, state the full-queue behaviour before implementing it. For a circular-array implementation, distinguish wraparound of indices from the logical order of waiting items.

What about several workers?

A container used by one small program is not automatically a safe multi-worker job system. Simultaneous access, duplicate processing, retries and crashes introduce requirements beyond a basic FIFO example.

For learning, keep the simulation single-threaded and use fictional jobs. Once the ordering is clear, you can study the separate mechanisms needed for reliable concurrent processing.

A test you can do on paper

Enqueue 4 and 8, dequeue once, enqueue 12, then dequeue twice. The removed values should be 4, 8 and 12, and the final queue is empty. A further dequeue must follow your empty-state policy.

Gammal Tech teaches Data Structures after C++. Use the queue exercise to explain arrival order, then compare the same operations with a stack. The different result comes from the structure's rule, even when the stored values are identical.

Choose your next step with Gammal Tech

Choose one skill you want to demonstrate, then review the relevant course and its prerequisites. Ask Gammal Tech about the content, study arrangements and current fees before enrolling.

References and course details

Use these references for further study. The examples and exercises in this article are educational; consult the catalogue for course details.

Articles & Guides