Queues and Stacks in Cpp tutorial
Queues Stacks in Cpp language
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.
A stack is a container adapter that works on the LIFO (Last In, First Out) principle. The element inserted last is removed first.
Function | Description |
---|---|
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 |
#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
A queue is a container adapter that works on the FIFO (First In, First Out) principle. The element inserted first is removed first.
Function | Description |
---|---|
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 |
#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
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
Feature | Stack (LIFO) | Queue (FIFO) |
---|---|---|
Access Order | Last in, first out | First in, first out |
Insertion | Performed at the top | Performed at the back |
Removal | Performed from the top | Performed from the front |
Use Cases | Expression evaluation, undo-redo | Task scheduling, buffers |
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.
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.