Queue in C++

Last updated: June 12, 2023

What is Queue?

a queue is an abstract data type that represents a collection of elements in a specific order. It follows the First-In-First-Out (FIFO) principle, meaning that the element that has been in the queue the longest is the first one to be removed.

Queue in C++

In C++, you can implement a queue using various data structures, such as arrays, linked lists, or the standard template library (STL) container called queue
Implementation of Queue in C++ example. In this tutorial we will learn to implement queue in C++ using the standard template library (STL) container called queue.

A step by step guide has been provided below explaining the implementation of queue in C++.

Step 1: Include the necessary headers.

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

Step 2: Create a queue object.

std::queue<int> myQueue;

Step 3: Adding elements to the queue

myQueue.push(10);
myQueue.push(20);
myQueue.push(30);

Step 4: Print the deque after performing operations:

 while (!myQueue.empty()) {
        // Print the element
        cout << myQueue.front();

        // Pop element from the queue
        myQueue.pop();

        // Check if there are more elements
        if (!myQueue.empty()) {
            cout << ", ";
        }
     }

 

Full Implementation:

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

int main() {
    queue<int> myQueue;

    myQueue.push(10);
    myQueue.push(20);
    myQueue.push(30);

cout<<"the queue is: ";
    while (!myQueue.empty()) {
        // Print the element
        cout << myQueue.front();

        // Pop element from the queue
        myQueue.pop();

        // Check if there are more elements
        if (!myQueue.empty()) {
            cout << ", ";
        }
    }

    return 0;
}

Output:

the queue is: 10, 20, 30

 

C++ Queue Methods 

In C++ there are a number of methods in queue that can be used to perform various functions. Some are listed below.

MethodDescription
push(const T& value)Inserts a new element of type T at the back of the queue.
pop()Removes the front element from the queue, reducing its size by one.
front()Returns a reference to the first (oldest) element in the queue.
back()Returns a reference to the last (newest) element in the queue.
empty()Checks if the queue is empty and returns true if it is, or false otherwise.
size()Returns the number of elements currently stored in the queue.
swap(queue<T>& other)Swaps the contents of two queues of the same type T.
emplace(Args&&... args)Constructs a new element of type T in-place at the back of the queue, using the given arguments forwarded to the constructor.

 

Insertion in Queue

Insertion in queue can be done following the push() method:

C++ implementation:

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

int main() {
    queue<int> myQueue;

    // Insertion (Enqueue) - Adding elements to the queue
    myQueue.push(10);
    myQueue.push(20);
    myQueue.push(30);

    // Displaying the elements in the queue
    cout << "Queue elements: ";
    while (!myQueue.empty()) {
        cout << myQueue.front() << " ";
        myQueue.pop();
    }
    cout << endl;

    return 0;
}

Output:

Queue elements: 10 20 30 

In this example insertion has been performed also called enqueue-ing by using the push() function to add elements to the queue. In this case, we insereted the values 10, 20, and 30 into the queue.

Removing Elements in Queue

Elements in queue can be removed done following the pop() method:

C++ implementation:

#include <iostream>
#include <queue>

int main() {
    queue<int> myQueue;

    myQueue.push(10);
    myQueue.push(20);
    myQueue.push(30);
    myQueue.push(40);
    myQueue.push(50);

    cout << "Original queue: ";
    while (!myQueue.empty()) {
        cout << myQueue.front() << " ";
        myQueue.pop();
    }

    cout << "\nQueue after removal: ";
    if (myQueue.empty()) {
        cout << "Queue is empty.";
    } else {
        cout << "Queue still has elements.";
    }

    return 0;
}

Output:

Original queue: 10 20 30 40 50 
Queue after removal: Queue is empty.

In this example a while loop was iterated through the queue until it becomes empty. We can also pop out one specific element if we want to, for that purpose the following code is to be executed-

Deleting element at a particular index in Queue in C++

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

int main() {
    queue<int> myQueue;

    myQueue.push(10);
    myQueue.push(20);
    myQueue.push(30);
    myQueue.push(40);
    myQueue.push(50);

    cout << "Original queue: ";
    queue<int> tempQueue = myQueue;  // Create a temporary queue for display purposes

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

    int choice;
    cout << "\nEnter the element index (starting from 0) to pop: ";
    cin >> choice;

    int queueSize = myQueue.size();

    if (choice >= 0 && choice < queueSize) {
        for (int i = 0; i < choice; i++) {
            myQueue.push(myQueue.front());
            myQueue.pop();
        }
        myQueue.pop();
        cout << "Element at index " << choice << " popped successfully.\n";
    } else {
        cout << "Invalid choice. No element popped.\n";
    }

    cout << "Queue after removal: ";
    while (!myQueue.empty()) {
        cout << myQueue.front() << " ";
        myQueue.pop();
    }

    return 0;
}

Output:

Original queue: 10 20 30 40 50 
Enter the element index (starting from 0) to pop: 2
Element at index 2 popped successfully.
Queue after removal: 40 50 10 20

In this code we popped an item at a particular index, here we popped element at index 2 which is 20.

Accessing elements in Queue

Insertion in queue can be done following the front() and back() method:

C++ implementation:

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

int main() {
    queue<int> myQueue;

    myQueue.push(10);
    myQueue.push(20);
    myQueue.push(30);

    // Storing the front elements
    queue<int> frontElements = myQueue;
 while (!myQueue.empty()) {
        // Print the element
        cout << myQueue.front();

        // Pop element from the queue
        myQueue.pop();

        // Check if there are more elements
        if (!myQueue.empty()) {
            cout << ", ";
        }
}
    // Accessing the front element
    myQueue.push(40);  // Adding an element for demonstration purposes
    cout << "\nFront element: " << frontElements.front() << endl;

    // Accessing the back element
    cout << "Back element: " << myQueue.back() << endl;

    // Modifying the front element
    frontElements.front() = 50;
    cout << "Modified front element: " << frontElements.front() << endl;

    return 0;
}

Output:

10, 20, 30
Front element: 10
Back element: 40
Modified front element: 50

Checking if the queue is empty

Insertion in queue can be done following the empty() method:

C++ implementation:

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

int main() {
    queue<int> myQueue;

    myQueue.push(10);
    myQueue.push(20);
    myQueue.push(30);

    if (myQueue.empty()) {
        cout << "Queue is empty." << endl;
    } else {
        cout << "Queue is not empty." << endl;
    }

    return 0;
}

Output:

Queue is not empty.

This example shows checks the number of elements in a queue, here as the queue is not empty it says that

"Queue is not empty" in the output.

 

Finding size of Deque

C++ implementation:

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

int main() {
    deque<int> myDeque;

    // Check if the deque is empty
    if (myDeque.empty()) {
        cout << "Deque is empty." << endl;
    } else {
        cout << "Deque is not empty." << endl;
    }

    // Add elements to the deque
    myDeque.push_back(10);
    myDeque.push_back(20);
    myDeque.push_back(30);

    // Get the size of the deque
    cout << "Deque size: " << myDeque.size() << endl;

    // Check if the deque is empty again
    if (myDeque.empty()) {
        cout << "Deque is empty." << endl;
    } else {
        cout << "Deque is not empty." << endl;
    }

    return 0;
}

Output:


Deque is empty.
Deque size: 3
Deque is not empty.

 

Related post