A Brief Introduction to Using Queues in C++

A Brief Introduction to Using Queues in C++

6 mins read463 Views Comment
Updated on Oct 17, 2023 14:02 IST

Queue in C++ is used to extract or process data in a particular order and manage multiple threads. In this article, we will discuss, different types of queues in C++ such as: Simple queue, priority queue, and circular queues with the help of examples.

2023_01_MicrosoftTeams-image-133.jpg

Table of Content

Recommended online courses

Best-suited C++ courses for you

Learn C++ with these high-rated online courses

– / –
4 months
4.24 K
6 weeks
– / –
15 days
– / –
– / –
– / –
1 hours
Free
2 hours
Free
1 hours
Free
1 hours

Introduction

A Queue in C++ is similar to the Queue in real life. You all must be part of some real-life queues, like a queue in school prayer, a queue in front of the ticket window, etc. 

For example, you want to buy your favourite ice cream, and you know there is a long queue to buy ice cream from the shop. What will you do? Go as early as possible; the one standing in the Queue will get the ice cream first. 

In a real-life queue, the person who enters first in the Queue will be the one who will exit first. Similarly, a queue in C++ also follows the same principle. It processes data in a FIFO order. 

Queue in C++ is used to extract or process data in a particular order and manage multiple threads. 

Also Read: Understanding Data Structure

In this tutorial, we will learn how queues are used in C++ with its several primary functions and a lot more. This article helps in getting the idea of using queues in C++ with examples and their different types. So, let’s start with me.

Types of Queues in C++

  • Simple Queue

It is the most basic Queue in data structure and uses the FIFO principle for inserting and removing data from the Queue.

  • Priority Queue

Like its name, this Queue has a priority associated with its elements. It follows the FIFO principle only when two data have similar priorities. Here, data can be removed either in ascending order or descending order. It is of two types: 

  1. Ascending Order Priority Queue: Data with the highest priority will be removed first.
  2. Descending Order Priority Queue: Data with the lowest priority will be removed first.
  • Circular Queue

In a Circular Queue, the Front and Rear ends are connected, forming a complete circle. A circular resolved the memory wastage drawback of the simple Queue in C++.

Also Read: Difference Between Stack and Queue

Also Read: What is C++

Now, let’s discuss them one-by-one in a complete detail.

Simple Queue in C++

Queue in C++ is a linear data structure to store and excess data in a particular order. In C++ and other programming languages, it uses FIFO (First In, First Out) principle for its processing.

According to this principle, the data inserted first into the Queue will be removed first from it.
In C++ stl (Standard Template Library) namespace is used to work with Queue, and you need to include a queue library to access all the in-built functions of the Queue by using the syntax: using namespace std, followed by a semi-colon.

Syntax of Queue:


 
Queue <data_type> queue_name
Copy code

You can use two data types: integer and string, for the Queue.

For example: 

  1. queue<int> queue_integer :- Create an integer data type queue with name queue_integer.
  2. queue<string> queue_string:- Create a string data type queue with name queue_string.

A Queue in C++ has two endpoints:

  1. Front: Elements/data are removed from this end.
  2. Rear: Elements/data are inserted from this end.
2023_01_image-88.jpg

Implementing Queue in C++


 
#include <iostream>
#include<queue>
using namespace std;
int main()
{
queue<int> queue_example;
}
Copy code

Primary Methods of the Queue in C++

There are various predefined Queue functions in the C++ library that can be directly used in the program for its processing. You can also define your own methods and override the predefined functions as per the requirements.

The mostly used Queue methods in C++ are as follows:

  1. push(): It is used to insert data into the Queue.
  2. pop(): It is used to remove elements from the Queue.
  3. size(): It will return the size of the Queue.
  4. front(): It will return the front value of the Queue.
  5. empty(): It returns the boolean value by checking whether the Queue is empty or not.
  6. deQueue(): This method is used to remove the data from the Queue.
  7. enQueue(): This method is used to add or insert the data into the Queue using push().

Now, let’s understand the above methods by implementing them in C++.

Implementing integer type Queue using C++


 
#include <iostream>
#include <queue>
using namespace std;
int main()
{
queue<int> number; //Integer type Queue declaration
//Inserting elements into the Queue using push() method
number.push(10);
number.push(20);
number.push(50);
number.push(40);
cout << "Queue is : ";
//While loop to print all elements of the queue using empty() method
while(!number.empty())
{
//printing elements at the front of the queue using front() method
cout << number.front() << ", ";
//popping elements from the Queue number
number.pop();
}
cout << endl;
return 0;
}
Copy code

OUTPUT
Queue is: 10, 20, 50, 40

Explanation: In this above example, we inserted the Queue elements using the push() method. You can not transverse it like an array for other data structures. To print all its elements, we used a while loop using the empty() method; the loop will terminate when the Queue satisfies the empty() condition.

The front() method will return the front() value. Using the pop() method to remove data from the Queue and print them.

Implementing String Type Queue in C++ and using pop() method


 
#include <iostream>
#include <queue>
using namespace std;
int main()
{
queue<string> fruits; //Integer type Queue declaration
//Inserting elements into the Queue using push() method
fruits.push(“Apple”);
fruits.push(“Grapes”);
fruits.push(“Banana”);
fruits.push(“Kivi”);
//this method will remove the first element of the Queue and print the Queue with remaining elements
fruits.pop();
cout << "Queue is : ";
//While loop to print all elements of the queue using empty() method
while(!fruits.empty())
{
//printing elements at the front of the queue using front() method
cout << fruits.front() << ", ";
//popping elements from the Queue number
fruits.pop();
}
cout << endl;
return 0;
}
Copy code

OUTPUT:

Queue is: Grapes, Banana, Kivi

Explanation: The Queue has apple, grapes, banana, and kivi as its contents. After calling the pop() method, it will remove the first fruit from the Queue i.e. apple.

The Queue will contain only the remaining 3 fruits as its elements.

Must Check: C++ Online Course and Certifications

Priority Queue in C++

Priority Queue is similar to the simple Queue in C++ with all the properties of a simple queue. It is a structured queue. Each element of the priority Queue in C++ has priorities, and based on those priorities, elements can be processed from it.

All its data are either arranged in increasing or decreasing order. Basically, in the Queue, the top element is served first, but the priority queue does not follow this principle and will process the element with the highest or lowest priority as per the type of priority queue.

Syntax:


 
priority_queue<data_type> queue_name
Copy code

Data type in the priority queue can be integer or string.

For example: priority_queue<int> integer_queue;

                      priority_queue<string> string_queue;

Methods of the priority queue

  • push(): It will insert elements into the Queue.
  • pop(): remove elements from the Queue.
  • size(): returns the queue size.
  • empty(): checks whether the Queue is empty or not. It returns a boolean value.
  • top(): It will return the highest priority member of the Queue.

Implementing a priority queue in C++


 
#include<iostream>
#include<queue>
for namespace std;
Int main()
{
priority_queue<int> p;
}
Implementing a priority queue in C++
#include <iostream>
#include <queue>
using namespace std;
int
main ()
{
priority_queue < int >queue1; //Integer type Queue declaration
//Inserting elements into the Queue using push() method
queue1.push (10);
queue1.push (2);
queue1.push (3);
queue1.push (4);
cout << "Priority Queue is : ";
//While loop to print all elements of the queue using empty() method
while (!queue1.empty ())
{
//printing queue elements with the highest priority
cout << queue1.top () << ", ";
//popping elements from the Queue number
queue1.pop ();
}
cout << endl;
return 0;
}
Copy code

OUTPUT

Priority Queue is: 10, 4, 3, 2

Circular Queue in C++

A circular queue is a queue with its endpoints connected with each other to manage memory wastage. It uses the FIFO principle for queue processing.
If we remove its element, that place is ready to be filled with new elements as the front and rear ends are connected. It has two points: Front and Rear.
The front is used to remove queue data. The rear end is used to insert data into the Queue.

2023_01_image-89.jpg

Methods of Circular Queue in C++

  • front(): Returns the data at the front place of the Queue
  • rear(): It returns the value at the rear place.
  • deQueue(): It is used to remove queue elements from the front end.
  • enQueue(): It is used to insert data into the Queue.

Conclusion 

Queues in C++ are implemented by using stl with different inbuilt functions. In this article, we implemented queues in C++ with different functions and learned how they behave.
We reached the end of this article, and I hope it is meaningful to you.

Contributed By: Sonal Meenu

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio