3.) Priority Queue (basic)
Write a program to check if priority queue is empty or not.
import java.util.*;
public class PriorityQueueIsEmptyExample {
public static void main(String[] args) {
// Create a priority queue
PriorityQueue<Integer> pq = new PriorityQueue<>();
// Check if the priority queue is empty
if (pq.isEmpty()) {
System.out.println("The priority queue is empty.");
} else {
System.out.println("The priority queue is not empty.");
}
// Add some elements to the priority queue
pq.add(5);
pq.add(2);
pq.add(10);
pq.add(7);
// Check if the priority queue is empty again
if (pq.isEmpty()) {
System.out.println("The priority queue is empty.");
} else {
System.out.println("The priority queue is not empty.");
}
}
}
Comments
Post a Comment