C++ Queues


queue in C++ is a linear data structure that follows the First In, First Out (FIFO) principle. This means the element that is added first will be the first one to be removed. You can think of it like a real-life queue of people waiting at a ticket counter — the person who arrives first is served first.

In C++, queues are implemented through the Standard Template Library (STL) and are defined in the <queue> header. The queue class is an adapter that works on top of other containers like deque or list.

Why Use a Queue in C++

Queues are widely used in programming for situations where you need to manage data in the order it arrives. Common real-world and technical examples include:

  • CPU task scheduling

  • Printer job queues

  • Breadth-first search (BFS) in graphs

  • Handling requests in order of arrival

Queues make sure that each element is processed in a fair and sequential manner.

Declaring a Queue

To create a queue, include the <queue> header and declare it using std::queue.

#include <iostream>
#include <queue>
using namespace std;

int main() {
    queue<int> numbers;
    numbers.push(10);
    numbers.push(20);
    numbers.push(30);

    cout << "Front element: " << numbers.front() << endl;
    cout << "Back element: " << numbers.back() << endl;

    return 0;
}

Explanation:
Here, we declared a queue named numbers. Elements are added using push(), and we used front() and back() to access the first and last elements respectively.

Common Queue Operations in C++

The C++ STL queue supports several key operations for working with elements efficiently.

1. push()

Adds a new element at the back of the queue.

numbers.push(40);

2. pop()

Removes the element at the front of the queue.

numbers.pop();

3. front()

Returns the first element (the one to be removed next).

cout << numbers.front();

4. back()

Returns the last element (the most recently added one).

cout << numbers.back();

5. empty()

Checks if the queue is empty.

if (numbers.empty()) cout << "Queue is empty";

6. size()

Returns the total number of elements in the queue.

cout << "Queue size: " << numbers.size();

Example: Basic Queue Operations

#include <iostream>
#include <queue>
using namespace std;

int main() {
    queue<string> q;

    q.push("Aditi");
    q.push("Meera");
    q.push("Riya");

    cout << "Front: " << q.front() << endl;
    cout << "Back: " << q.back() << endl;

    q.pop();
    cout << "After one pop, front is: " << q.front() << endl;

    return 0;
}

Output:

Front: Aditi
Back: Riya
After one pop, front is: Meera

Explanation:
The first element (“Aditi”) entered first and was removed first when we used pop() — this perfectly demonstrates the FIFO principle.

Accessing Elements in a Queue

Unlike vectors or lists, queues do not support random access or iteration. You can only access the front and back elements directly. If you need to view all elements, you can pop them one by one in a loop.

while (!q.empty()) {
    cout << q.front() << " ";
    q.pop();
}

Queue Implementation Using Different Containers

By default, queues use deque as the underlying container. However, you can also use a list for customized implementations.

queue<int, list<int>> customQueue;

This allows flexibility depending on how you want to manage memory or access behavior.

Real-Life Example: Simulating a Ticket Counter Queue

#include <iostream>
#include <queue>
using namespace std;

int main() {
    queue<string> tickets;

    tickets.push("Customer 1");
    tickets.push("Customer 2");
    tickets.push("Customer 3");

    while (!tickets.empty()) {
        cout << tickets.front() << " is being served." << endl;
        tickets.pop();
    }

    return 0;
}

Output:

Customer 1 is being served.
Customer 2 is being served.
Customer 3 is being served.

Explanation:
The first customer enters first and is served first, just like a real queue.

Advantages of Using Queue

  • Maintains the order of data (FIFO).

  • Simple and easy to implement.

  • Ideal for managing real-time data flow.

  • Useful in simulations, scheduling, and messaging systems.

Limitations of Queue

  • No random access to elements.

  • Traversing all elements requires removing them.

  • Slower than arrays for random reads or writes.

When to Use Queue

Use queues when:

  • Data must be processed in the same order it arrives.

  • You’re implementing scheduling systems or task management.

  • You need temporary holding before sequential processing.

Summary of the Tutorial

A C++ Queue is a powerful STL container designed for FIFO data management. It simplifies sequential processing, especially in systems where order matters. With simple operations like push(), pop(), front(), and back(), queues are both easy to use and efficient in handling structured data flow.


Practice Questions

  1. Write a C++ program to create a queue of integers and perform basic operations like push(), pop(), and front().

  2. Create a program that takes 5 names as input and stores them in a queue. Display all names in the order they were entered.

  3. Write a program to count the total number of elements in a queue using the size() function.

  4. Build a C++ program that removes all elements from a queue and checks whether it’s empty.

  5. Write a program that simulates a queue system for customers waiting at a bank counter.

  6. Create a program that reverses the elements of a queue using a stack.

  7. Write a C++ program to display all elements of a queue without modifying its order.

  8. Build a program that adds integers to a queue and removes only even numbers, displaying the remaining elements.

  9. Write a program that uses a queue to perform Breadth-First Search (BFS) on a simple graph.

  10. Create a C++ program that uses a queue to manage print jobs in order of arrival.


Go Back Top