QUEUE IN C++ STL

 QUEUE IN C++ STL

Introduction:

In this article, we are going to learn about the queue in C++ STL  and its operations in a detailed way with examples. Queue follows the FIFO (First In First Out) principle where elements that are added first will be removed first.

Queue:

The queue in the STL of C++ is a dynamically resizing container to implement the queue data structure. It is a sequential linear container that follows the First In First Out(FIFO) arrangement. If we try to insert an element even after the queue is full, then such a condition is known as overflow whereas, if we try to delete an element even after the queue is empty then such a condition is known as underflow.

Types of Queues:

Simple Queue: Simple queue also known as a linear queue is the most basic version of a queue.

Circular Queue:  In a circular queue, the element of the queue act as a circular ring. Its advantage is that the memory is utilized in a better way.  if no element is present at a certain position in the queue, then an element can be easily added at that position.

Priority Queue: This queue is a special type of queue. Its specialty is that it arranges the elements in a queue based on some priority.

Dequeue: Dequeue is also known as Double Ended Queue. As the name suggests double ended, it means that an element can be inserted or removed from both ends of the queue, unlike the other queues in which it can be done only from one end.

See also  Operator Overloading in c++

Advantages of Queue:

A large amount of data can be managed efficiently and with ease.

Operations such as insertion and deletion can be performed with ease as it follows the first in first out rule.

Queues are useful when a particular service is used by multiple consumers.

Queues are fast in speed for data inter-process communication.

Queues can be used in the implementation of other data structures.

Disadvantages of Queue:

The operations such as insertion and deletion of elements from the middle are time-consuming.

Limited Space.

In a classical queue, a new element can only be inserted when the existing elements are deleted from the queue.

Searching for an element takes O(N) time.

The maximum size of a queue must be defined prior.

Applications of Queue:

ATM Booth Line

Ticket Counter Line

Key press sequence on the keyboard

CPU task scheduling

Waiting time of each customer at call centers.

Implementation of Queue:

Sequential allocation:

A queue can be implemented using an array. It can organize a limited number of elements.

Linked list allocation:

 A queue can be implemented using a linked list. It can organize an unlimited number of elements.

C++ Queue Methods:

Methods Description                                                                 Time Complexity

push()             inserts an element at the back of the queue                    0(1)

pop()             removes an element from the front of the queue             0(1)

front()             returns the first element of the queue                              0(1)

back()              returns the last element of the queue                              0(1)

size()             returns the number of elements in the queue                  0(1)

empty( ) returns true if the queue is empty                                    0(1)

Queue Example Program:

#include <iostream>
#include <queue>
using namespace std;
void print_queue(queue<int> q)
{
	queue<int> temp = q;
	while (!temp.empty()) {
		cout << temp.front()<<" ";
		temp.pop();
	}
	cout << '\n';
}
// Drive Code
int main()
{
	queue<int> q1;
	q1.push(1);
	q1.push(2);
	q1.push(3);

	cout << "The first queue: ";
	print_queue(q1);
	
	queue<int> q2;
	q2.push(4);
	q2.push(5);
	q2.push(6);
	cout << "The second queue: ";
	print_queue(q2);
	q1.swap(q2);
	cout << "After swapping, the first queue: ";
	print_queue(q1);
	cout << "After swapping the second queue: ";
	print_queue(q2);
	cout<<q1.empty(); 
	return 0;
}

Output:

The first queue: 1 4 3 
The second queue: 9 8 7
After swapping, the first queue: 9 8 7 
After swapping the second queue: 1 4 3
0

So, That’s it for this tutorial. In the next article, we will look into some other concepts of queues.

See also   QUEUE IN C++ STL

If you have any queries regarding this, leave a comment below. Thank You! Happy Coding 🙂

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.