Extracting last element of a Priority Queue without traversing


Introduction

The Priority Queue in C++ is not similar to the normal Queue in the data structure, it has one difference: all its elements have priorities. We can extract its elements by traversing in the Queue.

But, here in this tutorial, we are trying a method for extracting the last element of the Priority Queue without traversing. Let’s start…

What is Priority Queue?

In data structure, the abstract data type is the priority queue. It is a queue where all its elements have some associated priorities. All its elements are removed based on their priorities. Higher priority data are extracted first as of fewer priority data. The queue data/elements can be integer or string, but no NULL value.

In case two elements have the same priority, then the priority queue will follow the FIFO (First In First Out) principle for extraction.

There are two types of Priority Queue to extract its elements −

  • Ascending Order Priority Queue − In this type of priority queue, elements are extracted in ascending order. The element with the minimum priority will be removed first.

  • Descending Order Priority Queue − In this type of priority queue, elements are extracted in increasing order. The element with the maximum priority will be removed first.

Syntax

 priority_queue<queue_type> queue_name

Extracting the Last Element of the Priority Queue without Traversing

Here, we are extracting the last element of the Priority Queue without traversing the whole. We are implementing the priority queue by binary tree. In the process following in-built methods are used −

  • size() − It returns the size of the Priority Queue.

    Syntax  queue_name.size()

  • insert() −Insert the elements into the Priority Queue.

    Syntax− queue_name.insert(data_type)

  • getMin() −It returns the smallest element of the Priority Queue.

    Syntax − queue_name.getMin()

  • getMax() −It returns the largest element of the Priority Queue.

    Syntax − queue_name.getMax()

  • isEmpty() −Returns true if Queue is empty.

  • deleteMin() −Delete the smallest Queue element.

    Syntax − queue_name.deleteMin()

  • deleteMax() − Delete the largest Queue element.

    Syntax − queue_name.deleteMax()

Algorithm

Step 1 − Create a Struct Class for the Queue operations.

Step 2 − Create a multiset for automatic sorting of the elements.

Step 3 − Insert elements into the Priority Queue.

Step 4 − Get the smallest and largest element without traversing by using in-built functions like getMin() and getMax().

Example

Code for extracting the last element from the Queue in C++

#include <bits/stdc++.h>
using namespace std;
  
// declaring a struct class for the Priority Queue
struct PQ  {
   multiset<int> s;
   
   //Getting the size of the Queue
   int size() { 
      return s.size(); 
   }
   
   //Checking Queue is empty or not
   bool isEmpty() { 
      return (s.size() == 0); 
   }
   
   void insert(int i) { 
      s.insert(i); 
   }
  
   //Method to get the smallest element of the Queue
   int getMin() { 
      return *(s.begin()); 
   }
   
   // Method to get the largest Queue element
   int getMax() { 
      return *(s.rbegin()); 
   }
   
   // Deleting Queue elements
   void deleteMin() {
      if (s.size() == 0)
         return;
      
      auto i = s.begin();
      s.erase(i);
   }
      
   // Method to delete the largest element
   void deleteMax() {
      if (s.size() == 0)
      return;
      
      auto i = s.end();
      i--;
      s.erase(i);
   }
};
  
//Main code
int main() {
   PQ p;   //initializing the Priority Queue
   
//inserting Queue elements
   p.insert(20);      
   p.insert(30);
   p.insert(50);
   p.insert(60);
   p.insert(90);
   
   cout << "Smallest Element is: " << p.getMin() << endl;
   cout << "Largest Element is: " << p.getMax() << endl;
   
   p.deleteMin();
   cout << "Smallest Element is: " << p.getMin() << endl;
   
   p.deleteMax();
   cout << "Largest Element is: " << p.getMax() << endl;
   
   cout << "Size of the Queue is: " << p.size() << endl;
   
   cout << "Queue is empty?: "
   << (p.isEmpty() ? "YES" : "NO") << endl;
   
   return 0;
}

Output

Smallest Element is: 20
Largest Element is: 90
Smallest Element is: 30
Largest Element is: 50
Queue is Empty?: NO

Conclusion

Priority Queue can be implemented through array, heap data structure, Linked List, and binary tree. It helps in exposing the hidden routes and various algorithms.

It is the end of this tutorial and I hope you find it meaningful.

Updated on: 22-Feb-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements