Open In App

Priority Queue of Lists in C++ with Examples

Last Updated : 10 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to print priority queue
// contents. Deliberately passing it
// call by value since we don't want
// to remove elements from the priority
// queue
void print(priority_queue<forward_list<int> > priorityQueue)
{
    while (!priorityQueue.empty()) {
        // Each element of the priority
        // queue is a forward list itself
        forward_list<int> st = priorityQueue.top();
  
        cout << "[ ";
  
        // Print the forward list elements
        for (auto element : st)
            cout << element << ' ';
        cout << ']';
        cout << '\n';
  
        // Pop out the topmost forward
        // list
        priorityQueue.pop();
    }
}
  
// Driver code
int main()
{
    // Declaring a priority queue of
    // forward list
    priority_queue<forward_list<int> > priorityQueue;
  
    // Declaring a forward list
    forward_list<int> forwardList1;
  
    // Inserting into the forward list
    forwardList1.push_front(2);
    forwardList1.push_front(4);
    forwardList1.push_front(6);
  
    // Inserting the forward list into
    // the priority queue
    priorityQueue.push(forwardList1);
  
    // Declaring another forward list
    forward_list<int> forwardList2;
  
    // Inserting into the forward list
    forwardList2.push_front(1);
    forwardList2.push_front(3);
    forwardList2.push_front(7);
  
    // Inserting the forward list into
    // the priority queue
    priorityQueue.push(forwardList2);
  
    // Declaring another forward list
    forward_list<int> forwardList3;
  
    // Inserting into the forward list
    forwardList3.push_front(11);
    forwardList3.push_front(22);
    forwardList3.push_front(33);
  
    // Inserting the forward list into
    // the priority queue
    priorityQueue.push(forwardList3);
  
    // Calling print function
    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++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to print priority queue
// contents. Deliberately passing it
// call by value since we don't want
// to remove elements from the priority
// queue
void print(priority_queue<list<int> > priorityQueue)
{
    while (!priorityQueue.empty()) {
        // Each element of the priority
        // queue is a list itself
        list<int> st = priorityQueue.top();
  
        cout << "[ ";
  
        // Print the list elements
        for (auto element : st)
            cout << element << ' ';
  
        cout << ']';
        cout << '\n';
  
        // Pop out the topmost list
        priorityQueue.pop();
    }
}
  
// Driver code
int main()
{
    // Declaring a priority queue of
    // lists
    priority_queue<list<int> > priorityQueue;
  
    // Declaring a list
    list<int> list1;
  
    // Inserting into the list
    // Pushing at the front in the
    list1.push_front(2);
  
    // Pushing at the back in the
    // list
    list1.push_back(4);
    list1.push_back(6);
  
    // Inserting the list into the
    // priority queue
    priorityQueue.push(list1);
  
    // Declaring another list
    list<int> list2;
  
    // Inserting into the list
    // Pushing at the front in the
    list2.push_front(2);
  
    // Pushing at the back in the
    // list
    list2.push_back(4);
    list2.push_back(7);
  
    // Inserting the list into the
    // priority queue
    priorityQueue.push(list2);
  
    // Declaring another list
    list<int> list3;
  
    // Inserting into the list
  
    // Pushing at the front in the
    list3.push_front(11);
  
    // Pushing at the back in the
    // list
    list3.push_back(22);
    list3.push_back(33);
  
    // Inserting the list into the
    // priority queue
    priorityQueue.push(list3);
  
    // Calling print function
    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
Previous
Next
Share your thoughts in the comments

Similar Reads