1. What Is a Queue?

Queue is a linear data structure that follows the principle:

FIFO – First In, First Out

So many people are already benefiting from this software development course

This means:

  • The first element added is the first one removed
  • Just like a real-life queue (bank line, food line)

Example (Real-Life Analogy)

Learn coding online

People in line:  A → B → C → D
Served first:    A

2. Basic Operations of a Queue

OperationDescription
enqueueAdd an element to the rear
dequeueRemove an element from the front
frontGet the front element
backGet the last element
isEmptyCheck if queue is empty
sizeNumber of elements

3. Queue in C++ (STL)

C++ provides the queue container in the Standard Template Library (STL).

Header File

#include <queue>

Declaration

queue<int> q;

4. Basic Queue Example

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

int main() {
    queue<int> q;

    q.push(10);   // enqueue
    q.push(20);
    q.push(30);

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

    q.pop(); // dequeue

    cout << "Front after pop: " << q.front() << endl; // 20
    cout << "Size: " << q.size() << endl;

    return 0;
}

5. Queue Functions Explained

FunctionPurpose
push(x)Insert element at rear
pop()Remove front element
front()Access first element
back()Access last element
empty()Returns true if empty
size()Returns number of elements

6. Visual Representation

Queue (FIFO)

Front → [10] [20] [30] ← Rear

pop() removes 10

7. Internal Working of STL Queue

  • STL queue is implemented using:
    • deque (default)
    • Can also be implemented with list
queue<int, deque<int>> q;
queue<int, list<int>> q;

❗ You cannot access elements randomly (no indexing like arrays).

8. Types of Queues

1. Simple Queue

Basic FIFO structure.

2. Circular Queue

  • Rear connects back to front
  • Efficient memory usage
  • Used in CPU scheduling

3. Priority Queue

Elements served based on priority, not order.

#include <queue>

priority_queue<int> pq;
pq.push(10);
pq.push(50);
pq.push(20);

// Top = 50

Max Heap by default

4. Double-Ended Queue (Deque)

Insert/remove from both ends.

#include <deque>

deque<int> dq;
dq.push_front(10);
dq.push_back(20);

9. Implementing Queue Using Array (Manual)

#define SIZE 5
int arr[SIZE];
int front = -1, rear = -1;

Enqueue

void enqueue(int x) {
    if (rear == SIZE - 1)
        cout << "Queue Overflow";
    else {
        if (front == -1) front = 0;
        arr[++rear] = x;
    }
}

Dequeue

void dequeue() {
    if (front == -1 || front > rear)
        cout << "Queue Underflow";
    else
        front++;
}

10. Implementing Queue Using Linked List

  • Each node has:
    • data
    • next

Advantages:

  • Dynamic size
  • No overflow (until memory full)

11. Time & Space Complexity

OperationTime Complexity
EnqueueO(1)
DequeueO(1)
Front/BackO(1)

Space Complexity:

  • O(n)

12. Applications of Queue

✔ CPU Scheduling
✔ Breadth-First Search (BFS)
✔ Print Queue
✔ Call Center Systems
✔ Message Queues
✔ Order Processing Systems

13. Queue vs Stack

StackQueue
LIFOFIFO
Push/PopEnqueue/Dequeue
Used in recursionUsed in scheduling

14. Common Mistakes

❌ Accessing queue elements using index
❌ Calling front() on empty queue
❌ Forgetting #include <queue>

15. When to Use Queue?

Use a queue when:

  • Order of processing matters
  • Tasks must be handled sequentially
  • Fair scheduling is required

Summary

queue in C++ is:

  • A FIFO data structure
  • Provided by STL for efficiency
  • Essential for algorithms and system design

View Comments

  • Nice explanation of the basic operations. One thing worth adding is how the `empty()` function is often used to prevent errors when trying to dequeue from an empty queue. It’s a small detail, but it can really help prevent runtime issues in more complex programs.

Recent Posts

The SOC Analyst

A SOC Analyst (Security Operations Center Analyst) is one of the frontline defenders of an organization’s cybersecurity…

6 days ago

Building an Offline-Friendly Image Upload System

Most image upload systems assume one thing: the user has a stable internet connection. That assumption…

2 weeks ago

JavaScript Background Sync API

Imagine a user submits a form while their internet connection suddenly disappears. Normally, the request…

2 weeks ago

10 Signs Your PC Has Been Hacked

Cybercriminals don't always announce their presence. Many compromises are designed to remain unnoticed for weeks…

3 weeks ago

What Killed jQuery?

For years, jQuery was everywhere. If you were building websites in the 2010s, there was a…

3 weeks ago

How to Resolve Git Merge Conflicts

Few things halt a developer’s flow faster than seeing the dreaded word: CONFLICT. A Git merge…

3 weeks ago