Open In App

Why does Queue have front but Priority-queue has top in stl?

Improve
Improve
Like Article
Like
Save
Share
Report

Why does the queue have front but the priority queue has top in stl?

The main difference between a queue and a priority queue is that a queue follows the FIFO (First-In-First-Out) principle, while a priority queue follows a specific priority order. In other words, the elements in a queue are processed in the order they were added, whereas the elements in a priority queue are processed based on their priority.

Queue:

A queue is a data structure that follows the FIFO (First-In-First-Out) principle, which means that the first element that is added to the queue is the first element that is removed. The elements in a queue are processed in the order they were added, and new elements are added to the back of the queue.

In the STL, the queue container is implemented as an adapter on top of other container classes, such as deque or list. The queue class provides a number of member functions, including the “front” member function, which returns a reference to the first element in the queue. This is the element that will be processed next when we call the “pop” member function to remove it from the queue.

For example: let’s say we have a queue of integers {1, 2, 3}. We can use the “front” member function to access the first element in the queue, which is 1:

C++




#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    queue<int> myQueue;
 
    myQueue.push(1);
    myQueue.push(2);
    myQueue.push(3);
 
    // Accessing first element
    int firstElement = myQueue.front();
    cout << firstElement << endl;
    return 0;
}


Java




// Java code implementation
 
import java.io.*;
import java.util.*;
 
class GFG {
    public static void main(String[] args)
    {
        Queue<Integer> myQueue = new LinkedList<Integer>();
 
        myQueue.add(1);
        myQueue.add(2);
        myQueue.add(3);
 
        // Accessing first element
        int firstElement = myQueue.peek();
        System.out.println(firstElement);
    }
}
 
// This code is contributed by sankar.


C#




// C# code implementation
using System;
using System.Collections.Generic;
 
public class GFG {
    public static void Main()
    {
        Queue<int> myQueue = new Queue<int>();
 
        myQueue.Enqueue(1);
        myQueue.Enqueue(2);
        myQueue.Enqueue(3);
 
        // Accessing first element
        int firstElement = myQueue.Peek();
        Console.WriteLine(firstElement);
    }
}
// This code is contributed by prasad264


Python3




from queue import Queue
 
myQueue = Queue()
 
myQueue.put(1)
myQueue.put(2)
myQueue.put(3)
 
# Accessing first element
firstElement = myQueue.queue[0]
print(firstElement)
 
# This code is contributed by Akash Jha


Javascript




let myQueue = [];
 
myQueue.push(1);
myQueue.push(2);
myQueue.push(3);
 
// Accessing first element
let firstElement = myQueue[0];
console.log(firstElement);
 
// This code is contributed by Akash Jha


Output

1

Time Complexity: O(1)
Auxiliary Space: O(1)

If we then call the “pop” member function to remove the first element from the queue, the next element in the queue (which is 2) becomes the first element, and we can access it using the “front” member function again:

C++




#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    queue<int> myQueue;
 
    myQueue.push(1);
    myQueue.push(2);
    myQueue.push(3);
 
    myQueue.pop();
    int newFirstElement = myQueue.front();
 
    cout << newFirstElement << endl;
    return 0;
}


Java




import java.util.Queue;
import java.util.LinkedList;
 
public class Main {
    public static void main(String[] args) {
        Queue<Integer> myQueue = new LinkedList<Integer>();
 
        myQueue.add(1);
        myQueue.add(2);
        myQueue.add(3);
 
        myQueue.remove();
        int newFirstElement = myQueue.peek();
 
        System.out.println(newFirstElement);
    }
}
 
// This code is contributed by Akash Jha


C#




using System;
using System.Collections.Generic;
 
class Program {
    static void Main(string[] args)
    {
        Queue<int> myQueue = new Queue<int>();
        myQueue.Enqueue(1);
        myQueue.Enqueue(2);
        myQueue.Enqueue(3);
 
        myQueue.Dequeue();
        int newFirstElement = myQueue.Peek();
 
        Console.WriteLine(newFirstElement);
    }
}
 
// This code is contributed by Akash Jha


Python3




from queue import Queue
 
myQueue = Queue()
 
myQueue.put(1)
myQueue.put(2)
myQueue.put(3)
 
myQueue.get()
newFirstElement = myQueue.queue[0]
 
print(newFirstElement)
 
# This code is contributed by Akash Jha


Javascript




const myQueue = [];
 
myQueue.push(1);
myQueue.push(2);
myQueue.push(3);
 
myQueue.shift();
const newFirstElement = myQueue[0];
 
console.log(newFirstElement);
 
// This code is contributed by Akash Jha


Output

2

Time Complexity: O(1)
Auxiliary Space: O(1)

Priority Queue

A priority queue, on the other hand, is a data structure that orders elements based on their priority. The elements in a priority queue are processed in order of their priority, with the highest-priority element processed first. New elements are added to the priority queue based on their priority order, and the highest-priority element is always at the front of the queue.

In the STL, the priority queue container is implemented as an adapter on top of a vector or a deque, and it requires a comparison function to determine the priority order of elements. The priority queue class provides a number of member functions, including the “top” member function, which returns a reference to the element with the highest priority in the queue. This is the element that will be processed next when we call the “pop” member function to remove it from the queue.

For example, let’s say we have a priority queue (Max-Heap) of integers {3, 1, 2}. We can use the “top” member function to access the element with the highest priority, which is 3:

C++




#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    priority_queue<int> myPriorityQueue;
 
    myPriorityQueue.push(3);
    myPriorityQueue.push(1);
    myPriorityQueue.push(2);
 
    int highestPriorityElement
        = myPriorityQueue.top();
    cout << highestPriorityElement << endl;
    return 0;
}


Java




import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        PriorityQueue<Integer> myPriorityQueue = new PriorityQueue<Integer>();
 
        myPriorityQueue.add(3);
        myPriorityQueue.add(1);
        myPriorityQueue.add(2);
 
        int highestPriorityElement = myPriorityQueue.peek();
        System.out.println(highestPriorityElement);
    }
}
// This code is contributed by Akash Jha


C#




using System;
using System.Collections.Generic;
 
class MainClass {
  public static void Main (string[] args) {
    PriorityQueue<int> myPriorityQueue = new PriorityQueue<int>();
 
    myPriorityQueue.Push(3);
    myPriorityQueue.Push(1);
    myPriorityQueue.Push(2);
 
    int highestPriorityElement = myPriorityQueue.Top();
    Console.WriteLine(highestPriorityElement);
  }
}
 
public class PriorityQueue<T> where T : IComparable<T> {
  private List<T> data;
 
  public PriorityQueue() {
    this.data = new List<T>();
  }
 
  public void Push(T item) {
    data.Add(item);
    int ci = data.Count - 1;
    while (ci > 0) {
      int pi = (ci - 1) / 2;
      if (data[ci].CompareTo(data[pi]) >= 0)
        break;
      T tmp = data[ci]; data[ci] = data[pi]; data[pi] = tmp;
      ci = pi;
    }
  }
 
  public T Top() {
    if (this.data.Count == 0)
      throw new InvalidOperationException("The priority queue is empty.");
    return data[0];
  }
}
 
// This code is contributed by Akash Jha


Python3




import queue
 
my_queue = queue.PriorityQueue()
 
my_queue.put(3)
my_queue.put(1)
my_queue.put(2)
 
highest_priority_element = my_queue.get()
 
print(highest_priority_element)
 
# This code is contributed by Akash Jha


Javascript




class PriorityQueue {
  constructor(compareFn) {
    this.compareFn = compareFn || ((a, b) => a - b);
    this.items = [];
  }
 
  push(item) {
    this.items.push(item);
    this.items.sort(this.compareFn);
  }
 
  pop() {
    return this.items.shift();
  }
 
  top() {
    return this.items[0];
  }
 
  size() {
    return this.items.length;
  }
 
  empty() {
    return this.items.length === 0;
  }
}
 
// Example usage
const myPriorityQueue = new PriorityQueue((a, b) => b - a);
 
myPriorityQueue.push(3);
myPriorityQueue.push(1);
myPriorityQueue.push(2);
 
const highestPriorityElement = myPriorityQueue.top();
console.log(highestPriorityElement);
 
// This code is contributed by Akash Jha


Output

3

Time Complexity: O(logN), Where N is the number of elements to be pushed.
Auxiliary Space: O(1)

If we then call the “pop” member function to remove the highest-priority element from the queue, the next element with the highest priority (which is 2) becomes the new top element, and we can access it using the “top” member function again:

C++




#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    priority_queue<int> myPriorityQueue;
 
    myPriorityQueue.push(3);
    myPriorityQueue.push(1);
    myPriorityQueue.push(2);
 
    myPriorityQueue.pop();
    int newHighestPriorityElement = myPriorityQueue.top();
    cout << newHighestPriorityElement << endl;
    return 0;
}


Java




import java.util.PriorityQueue;
 
public class Main {
    public static void main(String[] args) {
        PriorityQueue<Integer> myPriorityQueue = new PriorityQueue<Integer>();
 
        myPriorityQueue.add(3);
        myPriorityQueue.add(1);
        myPriorityQueue.add(2);
 
        myPriorityQueue.poll();
        int newHighestPriorityElement = myPriorityQueue.peek();
        System.out.println(newHighestPriorityElement);
    }
}
 
// This code is contributed by Akash Jha


Python3




import queue
 
myQueue = queue.PriorityQueue()
 
myQueue.put(3)
myQueue.put(1)
myQueue.put(2)
 
myQueue.get()
newHighestPriorityElement = myQueue.queue[0]
print(newHighestPriorityElement)
 
# This code is contributed by Akash Jha


C#




using System;
using System.Collections.Generic;
 
class MainClass {
  public static void Main (string[] args) {
    var myPriorityQueue = new PriorityQueue<int>();
 
    myPriorityQueue.Push(3);
    myPriorityQueue.Push(1);
    myPriorityQueue.Push(2);
 
    myPriorityQueue.Pop();
    int newHighestPriorityElement = myPriorityQueue.Top();
    Console.WriteLine(newHighestPriorityElement);
  }
}
 
class PriorityQueue<T> {
  private List<T> data;
  private Comparison<T> comparison;
 
  public PriorityQueue() : this(Comparer<T>.Default) {}
 
  public PriorityQueue(IComparer<T> comparer) : this(comparer.Compare) {}
 
  public PriorityQueue(Comparison<T> comparison) {
    this.data = new List<T>();
    this.comparison = comparison;
  }
 
  public void Push(T item) {
    int i = data.Count;
    data.Add(item);
    while (i > 0) {
      int p = (i - 1) / 2;
      if (comparison(data[p], item) <= 0)
        break;
      data[i] = data[p];
      i = p;
    }
    data[i] = item;
  }
 
  public T Pop() {
    T ret = data[0];
    T last = data[data.Count - 1];
    data.RemoveAt(data.Count - 1);
    if (data.Count > 0) {
      int i = 0;
      while (i * 2 + 1 < data.Count) {
        int a = i * 2 + 1, b = i * 2 + 2;
        if (b < data.Count && comparison(data[b], data[a]) < 0) {
          a = b;
        }
        if (comparison(data[a], last) >= 0) {
          break;
        }
        data[i] = data[a];
        i = a;
      }
      data[i] = last;
    }
    return ret;
  }
 
  public T Top() {
    if (data.Count == 0)
      throw new System.InvalidOperationException("Priority queue is empty.");
    return data[0];
  }
 
  public int Count {
    get { return data.Count; }
  }
}
 
// This code is contributed by Akash Jha


Javascript




let myPriorityQueue = [];
 
myPriorityQueue.push(3);
myPriorityQueue.push(1);
myPriorityQueue.push(2);
 
myPriorityQueue.sort((a, b) => b - a);
let highestPriorityElement = myPriorityQueue[0];
 
console.log(highestPriorityElement);
 
// This code is contributed by Akash Jha


Output

2

Time Complexity: O(logN), Where N is the number of elements to be pushed or poped.
Auxiliary Space: O(1)



Last Updated : 06 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads