Queues in C++ are part of the Standard Template Library (STL). A queue is a linear data structure that follows the FIFO (First In, First Out) principle.
What is a Queue?
A queue is a container where:
- The first element added is the first one removed
- Elements are inserted from the back (rear)
- Elements are removed from the front
Why Use Queues?
Queues are useful because they:
- Maintain order of processing
- Work like real-world waiting lines
- Help in task scheduling
- Are used in algorithms like BFS
Real-Life Example
Think of a ticket counter:
- First person in line is served first
- New people join at the end
- No one skips the line
This is exactly how a queue works.
Queue Header File
To use queues in C++, include the <queue> library.
#include <queue>
Declaring a Queue
queue<int> q;
Adding Elements (push)
Elements are added at the back of the queue.
q.push(10);
q.push(20);
q.push(30);
Removing Elements (pop)
Elements are removed from the front.
q.pop();
Accessing Elements
Front Element
cout << q.front();
Back Element
cout << q.back();
Example Program of Queue
#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<int> q;
q.push(1);
q.push(2);
q.push(3);
cout << "Front: " << q.front() << endl;
cout << "Back: " << q.back() << endl;
q.pop();
cout << "After pop, Front: " << q.front() << endl;
return 0;
}
Output
Front: 1
Back: 3
After pop, Front: 2
Common Queue Functions
| Function | Purpose |
|---|---|
push() | Add element at back |
pop() | Remove front element |
front() | Access first element |
back() | Access last element |
size() | Get number of elements |
empty() | Check if queue is empty |
How Queue Works
- Elements are inserted at the rear
- Elements wait in order
- First inserted is first removed
- Process continues sequentially
Types of Queues
1. Simple Queue
- Follows FIFO
- Basic queue structure
2. Circular Queue
- Last position connects to first
- Efficient memory usage
3. Priority Queue
- Elements are processed based on priority
- Not strictly FIFO
Difference Between Stack and Queue
| Stack | Queue |
|---|---|
| LIFO (Last In First Out) | FIFO (First In First Out) |
| Insert/remove from top | Insert rear, remove front |
| Used in recursion | Used in scheduling |
Important Points About Queues
- Works on FIFO principle
- Insertion happens at rear
- Deletion happens at front
- No random access allowed
Advantages of Queues
- Simple and efficient
- Maintains order
- Useful in scheduling systems
- Easy to implement using STL
Common Mistakes with Queues
- Trying to access middle elements
- Forgetting to check if queue is empty
- Calling
front()on empty queue - Confusing stack and queue behavior
Real-Life Applications
Queues are used in:
- CPU scheduling
- Print queue systems
- Customer service systems
- Network packet handling
- BFS algorithm in graphs
Why Queues are Important
Queues are important because they:
- Manage tasks in order
- Improve system efficiency
- Help in real-time processing
- Are widely used in system design
Conclusion
Queues in C++ are simple yet powerful STL containers that follow the FIFO principle. They are widely used in real-world applications like scheduling, processing tasks, and algorithm design where order matters.