Updated March 24, 2023
Introduction to Queue in C++
Queue is a kind of data structure that operates in the form of first in first out(FIFO) which means the element is going to be entered from the back and will be removed from the front. Like we have a general queue system in the practical world. The queue is a container adapter which holds data of the same type. Container adapter does not contain iterators so we cannot manipulate our data. The queue in c++ just provides us two methods for inserting elements and for removing element i.e. push() and pop().
Syntax:
template <class Obj, class Container = deque<Obj> > class queue;
Template Parameters:
- Obj: It represents the type of element it is going to contain.
- Container: Type of container object.
As we know queue is the container adapter so it should support the following operation which is mentioned below:
- back
- empty
- pop_front
- front
- size
- push_back
How Does Queue Work in C++?
As now we know that queue works in FIFO order. We can take an example of a ticket counter where whosoever entering the queue will be on the first most position and the first person who gets the tickets. Initially, the queue was empty then A enters the queue after that B enters so now A will be the one who is going to be removed first too. So this is FIFO. But in our technical language we can say like this:
- If we put any item in the queue then is it: ‘enqueue’.
- If we remove any item from the queue then it is: ‘dequeue’.
Operations of Queue
So it is the data structure that we follow so we can write it in any language. So we can say a queue is an object which allows us the following operations below:
- Peek: By this, we can get the value of the first element from the queue without removing it.
- Dequeue: This is the process of removing an element from the queue form front.
- Enqueue: This is the process of adding an element to the queue at the end.
- IsFull: It allows us to check if the queue is full.
- IsEmpty: It allows us to check if the queue is empty.
The operations which take place in Queue:
- We have a two-pointer in the queue which takes care of the front and end element in the queue which are: FRONT and REAR.
- For the first time when we try to initialize the queue, we kept the value for both this pointer i.e. REAR and FRONT as -1.
- when we enqueuing any element in the queue, we just increase the value of the REAR pointer and place this new element on this position.
- when we dequeuing any element from the queue return value of FRONT and increase the FRONT pointer.
- But before we enqueuing any element first we check the queue is already full or not.
- And now before dequeuing any element from the queue, we check if the queue is already empty or not.
- So on enqueuing the very first element, we set the FRONT pointer value to 0.
- So on dequeuing the very last element again, we reset the value for both pointer i.e. FRONT and REAR to -1 and the process continues.
But there is some limitation of the queue is like sometimes the size of the queue got reduced and the only solution we have is to reset the queue again.
Example of Queue in C++
Let us see the example of queue in C++ with code implementation and output.
Code:
#include <iostream>
#include <queue>
using namespace std;
void queueDemoshow(queue <int> gq1)
{
queue <int> g1 = gq1;
while (!g1.empty())
{
cout << '\t' << g1.front();
g1.pop();
}
cout << '\n';
}
int main()
{
queue <int> queuedemo;
queuedemo.push(10);
queuedemo.push(20);
queuedemo.push(30);
cout << "elements in the queue are : ";
queueDemoshow(queuedemo);
cout << "\nPrinting the size of the queue (queuedemo.size()) : " << queuedemo.size();
cout << "\nPrinting the first elemnt from the queue (queuedemo.front()) : " << queuedemo.front();
cout << "\nPrintitng the last element from the queue (queuedemo.back()) : " << queuedemo.back();
cout << "\nUse of pop () method (queuedemo.pop()) : ";
queuedemo.pop();
queueDemoshow(queuedemo);
return 0;
}
Output:
Queue Member Types in C++
The queue member types in C++ are as follows,
- value_type: This is used to represent the type for the elements which are going to insert in the queue.
- container_type: This is used to specify the container type.
- size_type: This is used to specifies the size of the elements in the queue.
- reference: This is used to specifies what is going to be the reference type for the container.
- const_reference: This is the reference for the constant container.
Queue Functions in C++
Queue provides us some function to manipulate our variable or object to perform some action. Some of the functions are mentioned below which are as follows:
- swap: This function is used to swap the elements. It generally interchanges the elements.
- size: This function is used to know the size of the queue. It will calculate the number of the element present in the queue.
- empty: This function is used to check if the queue is empty or not. it will return a Boolean value. If there is no element present in the queue then it will return true else it will return false.
- emplace: This function will insert a new element. But this new element will be added one position above to the REAR element, not to the end.
- pop: As we know this method will remove an element from the queue and the element will be removed from the FRONT because it follows FIFO.
- push: This will add a new element to the queue and this element is going to add at the end because it follows FIFO.
- back: This we used to access the last element in the queue i.e. REAR element. This is important because all insertion happens at the end.
- front: This we used to access the first element. This is important because all the removal of element happens at FRONT only.
Non-Member Function
- Relational Operators: It provides the relational operators that are going to use in the queue.
- uses_allocator<queue>: This function is used, allocator.
Conclusion
C++ queue works in the FIFO technique. It is a data structure only the C++ syntax is a different process is the same. We have FRONT and REAR as an important keyword in this.
Recommended Articles
This is a guide to Queue in C++. Here we discuss the Introduction and how does queue work in C++ along with an example and its queue member types, functions. You may also look at the following articles to learn more –