10.) Priority Queue (basic)
write a programe to remove all element from a priority queue except smallest one.
import java.util.PriorityQueue;
public class RemovePriorityQueueElements {
public static void main(String[] args) {
// Creating a priority queue
PriorityQueue<Integer> pq = new PriorityQueue<>();
// Adding elements to the priority queue
pq.add(10);
pq.add(20);
pq.add(15);
pq.add(30);
pq.add(25);
// Printing the original priority queue
System.out.println("Original Priority Queue: " + pq);
// Removing all elements except the smallest one
while (pq.size() > 1) {
pq.remove();
}
// Printing the priority queue after removing elements
System.out.println("Priority Queue after removing all elements except smallest one: " + pq);
}
}
Comments
Post a Comment