Turn a Queue into Priority Queue


Introduction

A queue is a linear data structure that follows the FIFO principle for inserting and removing elements and has no close ending. It is functional on both ends. In this tutorial, we will learn how to turn a queue into a priority Queue and understand the meaning of queue and priority queue in the data structure.

What is Queue?

Queue in data structure resembles the queue in real life and is used to handle multiple data. It is an ordered list in which elements are entered on the rear end and removed from the front end. In this, the element which enters first in the queue, will be removed first.

Syntax to declare Queue

queue<data type> name 

What is a Priority Queue?

A priority Queue is a structured Queue and has an associated priority for each element. The elements from the priority queue are removed based on their defined priority. If two elements have the same priority, then it will follow FIFO (First In First Out) principle.

Priority in the queue is the value of each element. The element with the highest priority remains at the front or top, and the other elements as per priority will dequeue.

Syntax to declare Priority Queue

priority_queue<data type> name

Types of Priority Queue

1. Ascending Order Priority Queue − In this priority queue, the elements are arranged in decreasing order. The element with the lowest value has the highest priority. For example: {2,6,8,9} this is an ascending order priority queue with 2 as the highest priority.

2. Descending Order Priority Queue − All elements in this queue are arranged in increasing order. The element with the largest value has the highest priority. For example: {8,6,5,4,2} this descending order queue with element 8 has the highest priority.

Converting Queue into Priority Queue

We are converting a queue into a priority queue by arranging the queue in decreasing order. We are using three user-defined functions and some built-in functions of the C++ library.

Methods used in the process

  • push() − Used to insert queue data.

    Syntax − queue_name.push();

  • pop() − To pop the queue elements.

    Syntax − queue_name.pop();

  • empty() − It checks whether the queue is empty or not and returns True if the queue is empty.

    Syntax − queue_name.empty();

  • front() − It returns the front value of the queue.

    Syntax − queue_name.front();

Algorithm

Step 1 − Insert elements into the queue.

Step 2 − Sort the queue by checking if the queue is empty or not. If the queue is not empty pop the element and return if the queue is empty.

Step 3 − Add elements by checking their front values.

Step 4 − Recursively call the functions.

Example

The code to convert queue into priority queue is as −

#include <bits/stdc++.h>
using namespace std;
   
void frontelement(queue<int>& q1, int q1size)     //function to check the front value of the queue
{
   if (q1size <= 0)                                                   // checking the queue size
   return;
   
   q1.push(q1.front());
   q1.pop();
   
   frontelement(q1, q1size - 1);
}
   
void addelement(queue<int>& q1, int val1, int q1size) //function to add elements in queue
{
   if (q1size == 0 || q1.empty()) {
      q1.push(val1);
      return;
   } 
   
   else if (val1 >= q1.front()) {
      
      q1.push(val1);
    
      frontelement(q1, q1size);
   } else {
   
      q1.push(q1.front());
      q1.pop();
   
      addelement(q1, val1, q1size - 1);
   }
}
   
void sortqueue(queue<int>& q1)    //function for queue sorting
{
   if (q1.empty()) {
      return;
   }
   
   int element = q1.front();
   q1.pop();
   sortqueue(q1);
   addelement(q1, element, q1.size());
}

// main code
int main()
{
   queue<int> q1;           //initializing queue
     
   //adding elements in the queue
   q1.push(4);
   q1.push(6);
   q1.push(5);
   q1.push(9);
   q1.push(3);
   q1.push(7);
   
   sortqueue(q1);
   
   while (!q1.empty()) {
      cout << q1.front() << " ";
      q1.pop();
   }
   
   return 0;
}

Output

9 7 6 5 4 3                                

Explanation of the above code

  • Elements are added in the queue q1 using push() function and the queue contains {4,6,5,9,3,7}.

  • Sortqueue() function is called to sort the queue q1 in descending order.

    • If the queue q1 is empty and it returns.

    • If queue q1 contains a value, it stores the front value in a variable and pop it out.

  • Insertqueue() function is declared to insert the values in the queue.

    • It first checks whether queue q1 is empty or not.

    • If the push value is greater than value at the front, push the value and recursively call the function to check all inserted values.

  • Frontelement() function checks if queue q1 is empty or not, if not push the front value and pop it to the last of the queue q1.

  • Recursively call the function.

Conclusion

Priority queue is similar to the queue in data structure with only difference of priority of each element in the queue. Normal queue pop all elements using first-in first-out principle while priority queue removes elements either by ascending or descending order.

Updated on: 22-Feb-2023

979 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements