Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

C++ Program to Implement Queue in STL



Queue is a linear data structure that follows the First In First Out (FIFO) principle. In this article, we will learn how to use the queue container from the Standard Template Library (STL) in C++.

What is Queue?

A Queue is a container that stores data in a sequential way. Meaning, the data that is inserted first to the queue is the one to be removed first from it. This is same as a queue of people where, the person who comes first to the queue will served first. The STL library of C++ provides a pre-defined queue container adapter that is based on the deque container by default.

For example, in the code we have shown how data are inserted and removed in queue:

// Declare a queue
queue<int> q;
// Add Data
q.push(10);
q.push(20);

q.pop(); // This operation will remove 10 from queue

Using queue Container in STL

The queue container is defined in the <queue> header of STL. It provides a FIFO data structure with basic operations such as push, pop, front, and back. Below are some points about this container:

  • Header: <queue>
  • Syntax:
    queue<datatype> queue_name;
  • Common functions:
  • push() - Function used to insert an element at the end of the queue.
  • pop() - Function used to remove an element from the front of the queue.
  • front() - Function used to access an the front element of the queue.
  • back() - Function used to access an the last element of the queue.
  • empty() - Function used to check an if the queue is empty.
  • size() - Function used to return an the size the queue.

Steps to Implement Queue in C++ STL

Following are steps/algorithm to use queue using C++ STL:

  • Include the <queue> header file.
  • Declare a queue with desired data type.
  • Use push() to insert elements.
  • Use pop() to remove elements from front.
  • Access elements using front() and back().
  • Use empty() and size() to check status.

C++ Program to Implement Queue using STL

The below code is the implementation of the above algorithm in C++ language.

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

int main() {
    queue<int> q;

    // Insert elements
    q.push(10);
    q.push(20);
    q.push(30);

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

    // Remove one element
    q.pop();

    cout << "After one pop operation:" << endl;
    cout << "Front Element: " << q.front() << endl;
    cout << "Queue Size: " << q.size() << endl;

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

    return 0;
}

The output of above code will be

Front Element: 10
Back Element: 30
After one pop operation:
Front Element: 20
Queue Size: 2
Queue is not empty

Time and Space Complexity

Time Complexity:

  • push(), pop(), front(), back(): O(1) for each operation.

Space Complexity: O(n), where n is the number of elements in the queue.

Updated on: 2025-05-09T15:39:16+05:30

783 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements