A 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:
People in line: A → B → C → D
Served first: A | Operation | Description |
|---|---|
enqueue | Add an element to the rear |
dequeue | Remove an element from the front |
front | Get the front element |
back | Get the last element |
isEmpty | Check if queue is empty |
size | Number of elements |
C++ provides the queue container in the Standard Template Library (STL).
#include <queue> queue<int> q; #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;
} | Function | Purpose |
|---|---|
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 |
Queue (FIFO)
Front → [10] [20] [30] ← Rear
pop() removes 10 queue is implemented using: deque (default)listqueue<int, deque<int>> q;
queue<int, list<int>> q; ❗ You cannot access elements randomly (no indexing like arrays).
Basic FIFO structure.
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
Insert/remove from both ends.
#include <deque>
deque<int> dq;
dq.push_front(10);
dq.push_back(20);
#define SIZE 5
int arr[SIZE];
int front = -1, rear = -1; void enqueue(int x) {
if (rear == SIZE - 1)
cout << "Queue Overflow";
else {
if (front == -1) front = 0;
arr[++rear] = x;
}
} void dequeue() {
if (front == -1 || front > rear)
cout << "Queue Underflow";
else
front++;
} datanextAdvantages:
| Operation | Time Complexity |
|---|---|
| Enqueue | O(1) |
| Dequeue | O(1) |
| Front/Back | O(1) |
Space Complexity:
✔ CPU Scheduling
✔ Breadth-First Search (BFS)
✔ Print Queue
✔ Call Center Systems
✔ Message Queues
✔ Order Processing Systems
| Stack | Queue |
|---|---|
| LIFO | FIFO |
| Push/Pop | Enqueue/Dequeue |
| Used in recursion | Used in scheduling |
❌ Accessing queue elements using index
❌ Calling front() on empty queue
❌ Forgetting #include <queue>
Use a queue when:
A queue in C++ is:
Latest tech news and coding tips.
A SOC Analyst (Security Operations Center Analyst) is one of the frontline defenders of an organization’s cybersecurity…
Most image upload systems assume one thing: the user has a stable internet connection. That assumption…
Imagine a user submits a form while their internet connection suddenly disappears. Normally, the request…
Cybercriminals don't always announce their presence. Many compromises are designed to remain unnoticed for weeks…
For years, jQuery was everywhere. If you were building websites in the 2010s, there was a…
Few things halt a developer’s flow faster than seeing the dreaded word: CONFLICT. A Git merge…
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.