Priority Queue of Lists in C++ with Examples
Last Updated :
10 Nov, 2021
Priority Queue
Priority queues are a type of container adapters, specifically designed such that the first element of the queue is the greatest of all elements in the queue and elements are in nonincreasing order (hence we can see that each element of the queue has a priority {fixed order}).
Functions used with Priority Queue:
- empty(): This function returns whether the queue is empty.
- size(): This function returns the size of the queue.
- top(): Returns a reference to the topmost element of the queue.
- push(x): This function adds the element ‘x’ at the end of the queue.
Lists
Lists are sequence containers that allow non-contiguous memory allocation. As compared to vector, the list has slow traversal, but once a position has been found, insertion and deletion are quick. Normally, when we say a List, we talk about a doubly linked list. For implementing a singly linked list, we use a forward list.
Functions used with Lists:
- push_front(x): Adds a new element ‘x’ at the beginning of the list.
- push_back(x): Adds a new element ‘x’ at the end of the list.
Forward Lists
Forward list in STL implements singly linked list. Introduced from C++11, forward lists are more useful than other containers in insertion, removal, and moving operations (like sort) and allow time constant insertion and removal of elements. It differs from the list by the fact that the forward list keeps track of the location of only the next element while the list keeps track of both the next and previous elements, thus increasing the storage space required to store each element. The drawback of a forward list is that it cannot be iterated backward and its individual elements cannot be accessed directly.
Forward List is preferred over the list when only forward traversal is required (same as singly linked list is preferred over doubly linked list) as we can save space. Some example cases are, chaining in hashing, adjacency list representation of the graph, etc.
Functions used with Forward list:
- push_front(x): Adds a new element ‘x’ at the beginning of the list.
This article focuses on how the priority queue of forward list and list can be used in C++. Priority queue of lists and forward lists can be quite useful while designing complex data structures.
Below is the implementation of the priority queue of forward list:
C++
#include <bits/stdc++.h>
using namespace std;
void print(priority_queue<forward_list< int > > priorityQueue)
{
while (!priorityQueue.empty()) {
forward_list< int > st = priorityQueue.top();
cout << "[ " ;
for ( auto element : st)
cout << element << ' ' ;
cout << ']' ;
cout << '\n' ;
priorityQueue.pop();
}
}
int main()
{
priority_queue<forward_list< int > > priorityQueue;
forward_list< int > forwardList1;
forwardList1.push_front(2);
forwardList1.push_front(4);
forwardList1.push_front(6);
priorityQueue.push(forwardList1);
forward_list< int > forwardList2;
forwardList2.push_front(1);
forwardList2.push_front(3);
forwardList2.push_front(7);
priorityQueue.push(forwardList2);
forward_list< int > forwardList3;
forwardList3.push_front(11);
forwardList3.push_front(22);
forwardList3.push_front(33);
priorityQueue.push(forwardList3);
print(priorityQueue);
return 0;
}
|
Output
[ 33 22 11 ]
[ 7 3 1 ]
[ 6 4 2 ]
Below is the implementation of the priority queue of lists:
C++
#include <bits/stdc++.h>
using namespace std;
void print(priority_queue<list< int > > priorityQueue)
{
while (!priorityQueue.empty()) {
list< int > st = priorityQueue.top();
cout << "[ " ;
for ( auto element : st)
cout << element << ' ' ;
cout << ']' ;
cout << '\n' ;
priorityQueue.pop();
}
}
int main()
{
priority_queue<list< int > > priorityQueue;
list< int > list1;
list1.push_front(2);
list1.push_back(4);
list1.push_back(6);
priorityQueue.push(list1);
list< int > list2;
list2.push_front(2);
list2.push_back(4);
list2.push_back(7);
priorityQueue.push(list2);
list< int > list3;
list3.push_front(11);
list3.push_back(22);
list3.push_back(33);
priorityQueue.push(list3);
print(priorityQueue);
return 0;
}
|
Output
[ 11 22 33 ]
[ 2 4 7 ]
[ 2 4 6 ]
By default priority queue is a max-heap, therefore internally for two lists/ forward lists inside the priority queue, the list/ forward list having the greater first element is the topmost element. If the first element is equal then the second value of the list/ forward list is compared and so on.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...