Have the PriorityQueue class you created for Seminar #3 implement the Iterable i
ID: 3576269 • Letter: H
Question
Have the PriorityQueue class you created for Seminar #3 implement the Iterable interface (which means it provides an iterator( ) method that returns an iterator). The iterator that the PriorityQueue will provide should be defined by a private inner class that implements the Iterator interface (which means it provides definitions for hasNext( ) and next( ). You do NOT need to provide a definition for the remove method.Create a driver program that tests the new PriorityQueue class. Use the for-each loop to display the patients in the queue. Then use a hasNext( ) loop, and the next( ) method, to display the patients in the queue once again.(Data structure java question)
Explanation / Answer
import java.util.*;
// class declaration
class PriorityQueue {
public static void main(String args[]){
//priority queue
PriorityQueue<String> queue=new PriorityQueue<String>();
//adding patient names
queue.add("lakshmi");
queue.add("sami");
queue.add("priya");
queue.add("Jaya");
queue.add("paddu");
System.out.println("head:"+queue.element());
System.out.println("head:"+queue.peek());
System.out.println("iterating the queue elements:");
Iterator itr=queue.iterator();
// displaying queue elements using iterator
while(itr.hasNext()){
System.out.println(itr.next());
}
queue.remove();
queue.poll();
System.out.println("after removing two elements:");
Iterator<String> itr2=queue.iterator();
// displaying queue elements after removing first 2 elements
while(itr2.hasNext()){
System.out.println(itr2.next());
}
}
}
Output:
head:Jaya
head:Jaya
iterating the queue elements:
Jaya
lakshmi
priya
sami
paddu
after removing two elements:
paddu
sami
priya
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.