Queues and Stacks in Cpp tutorial

9/18/2025

Queues Stacks in Cpp language

Go Back

Queues and Stacks in C++

Introduction

Queues and Stacks are fundamental container adapters provided by the Standard Template Library (STL) in C++. They allow developers to handle data in specific orders — FIFO (First-In-First-Out) for queues and LIFO (Last-In-First-Out) for stacks. Understanding these structures is essential for solving problems like expression evaluation, task scheduling, and data buffering.

This article covers the concepts, usage, and examples of queues and stacks in C++ for beginners.


Queues  Stacks in Cpp language

Stack in C++

A stack is a container adapter that works on the LIFO (Last In, First Out) principle. The element inserted last is removed first.

Key Functions of Stack

FunctionDescription
push()Inserts an element at the top
pop()Removes the top element
top()Returns the top element
size()Returns the number of elements
empty()Checks if the stack is empty

Example: Using Stack

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

int main() {
    stack<int> st;
    st.push(10);
    st.push(20);
    st.push(30);

    cout << "Top element: " << st.top() << "\n";

    while (!st.empty()) {
        cout << st.top() << " ";
        st.pop();
    }
    return 0;
}

Output:

Top element: 30
30 20 10

Queue in C++

A queue is a container adapter that works on the FIFO (First In, First Out) principle. The element inserted first is removed first.

Key Functions of Queue

FunctionDescription
push()Adds an element at the back
pop()Removes an element from the front
front()Returns the first element
back()Returns the last element
size()Returns the number of elements
empty()Checks if the queue is empty

Example: Using Queue

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

int main() {
    queue<string> q;
    q.push("Alice");
    q.push("Bob");
    q.push("Charlie");

    cout << "Front: " << q.front() << ", Back: " << q.back() << "\n";

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

Output:

Front: Alice, Back: Charlie
Alice Bob Charlie

Priority Queue (Max-Heap)

A priority queue is a type of queue where elements are stored in sorted order (by default max-heap). The largest element is always at the top.

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

int main() {
    priority_queue<int> pq;
    pq.push(10);
    pq.push(30);
    pq.push(20);

    while (!pq.empty()) {
        cout << pq.top() << " ";
        pq.pop();
    }
    return 0;
}

Output: 30 20 10


Difference Between Stack and Queue

FeatureStack (LIFO)Queue (FIFO)
Access OrderLast in, first outFirst in, first out
InsertionPerformed at the topPerformed at the back
RemovalPerformed from the topPerformed from the front
Use CasesExpression evaluation, undo-redoTask scheduling, buffers

When to Use Stack vs Queue

  • Use stack when you need to access the most recent element first.

  • Use queue when you need to process elements in the order they arrive.

  • Use priority_queue when element order depends on priority values.


Conclusion

Stacks and queues are essential data structures in C++ for organizing and processing data efficiently. STL provides built-in implementations that are fast, reliable, and easy to use, saving developers from implementing them from scratch.