Queue Implementation in C++ - Techie Delight
Queue Implementation in C++ - Techie Delight
All Problems Array Tree Linked List DP Graph Backtracking Matrix Heap
A queue is a linear data structure that serves as a container of objects that are inserted and
removed according to the FIFO (First–In, First–Out) principle.
Queue has three main operations: enqueue , dequeue , and peek . We have already covered
these operations and C implementation of queue data structure using an array and linked list.
In this post, we will cover queue implementation in C++ using class and STL.
1 #include <iostream>
2 #include <cstdlib>
3 using namespace std;
4
5 // Define the default capacity of a queue
6 #define SIZE 10
7
8 // A class to store a queue
9 class queue
This website{ uses cookies. By using this site you agree to the use of cookies, our
10 Close and accept
policies,
11 copyright terms
int and other conditions.
*arr; // array Read our Privacy
to store queue Policy
elements
https://www.techiedelight.com/queue-implementation-cpp/ 1/5
5/18/2021 Queue Implementation in C++ – Techie Delight
Output:
Inserting 1
Inserting 2
Inserting 3
The front element is 1
Removing 1
Inserting 4
The queue size is 3
Removing 2
Removing 3
Removing 4
The queue is empty
Using std::queue :
C++’s STL provides a std::queue template class which is restricted to only enqueue/dequeue
operations. It also provides std::list which has push_back and pop_front operations
with LIFO semantics. Java’s library contains Queue interface that specifies queue operations.
std::queue
1 #include <iostream>
2 #include <queue>
3 using namespace std;
4
5 // Queue implementation in C++ using `std::queue`
6 int main()
7 {
8 queue<string> q;
9
10 q.push("A"); // Insert `A` into the queue
11 q.push("B"); // Insert `B` into the queue
12 q.push("C"); // Insert `C` into the queue
13 q.push("D"); // Insert `D` into the queue
This website
14 uses cookies. By using this site you agree to the use of cookies, our Close and accept
policies, copyright terms and other conditions. Read our Privacy Policy
https://www.techiedelight.com/queue-implementation-cpp/ 4/5
5/18/2021 Queue Implementation in C++ – Techie Delight
std::list
Output:
Also See:
Techie Delight © 2021 All Rights Reserved. Privacy Policy Contact us Tools
This website uses cookies. By using this site you agree to the use of cookies, our Close and accept
policies, copyright terms and other conditions. Read our Privacy Policy
https://www.techiedelight.com/queue-implementation-cpp/ 5/5