8.) Priority Queue (basic)
Write a programe to search for a element in a priority queue.
import java.util.*;
public class PriorityQueueSearch {
public static void main(String[] args) {
// Create a priority queue and add some elements
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
pq.add(3);
pq.add(1);
pq.add(5);
pq.add(2);
pq.add(4);
// Print the priority queue
System.out.println("Priority Queue: " + pq);
// Search for an element in the priority queue
int searchElement = 2;
boolean found = pq.contains(searchElement);
// Print the search result
if (found) {
System.out.println(searchElement + " is present in the priority queue.");
} else {
System.out.println(searchElement + " is not present in the priority queue.");
}
}
}
Comments
Post a Comment