Create a LinkedPriorityQueue class in Java that inherits from the LinkedQueue cl
ID: 3788450 • Letter: C
Question
Create a LinkedPriorityQueue class in Java that inherits from the LinkedQueue class.
The LinkedPriorityQueue class implements enqueue differently (that is, it overrides the enqueue method). Also since the LinkedQueue class is now being used in inheritance, make its instance variables protected so the child class can access them.
In the LinkedPriorityQueue class the generic type parameter declaration will need to change to “T extends Comparable”. Also include a remove method in your LinkedPriorityQueue class. This method should receive a generic parameter, T, and uses the equals method to match this parameter with the node in the priority queue that contains this information. You’ll also need to create a Patient class, that implements the Comparable interface, and holds the patient’s name and the priority assigned to their injury (10 = extremely severe … 1 = mild).
The compareTo method will use the priority to determine <, ==, or >. (Simplest way is to subtract the priorities and return that value). The Patient class will also need to implement the equals method, for use in the remove code, and should use the patient’s name as the basis for the comparison (since the name is a String, use the equals method from the String class). A patient leaves the priority queue because the wait is taking too long or they’ve succumbed to their injury. Test your LinkedPriorityQueue class by adding Patient objects according to their priority (highest priority appears first in the queue).
Be sure to add the objects in random order by their priority. Print out the queue after you’ve added about 7 patients. Then try removing a few patients, and adding a few more. Print out the final queue.
(Updated question, language is Java, sorry I forgot to mention that!)
Explanation / Answer
import java.util.*;
class Example
{
public static void main(String args[])
{
// Creating empty linked queue
LinkedQueue<String> pQueue = new LinkedQueue<String>();
// Adding items to the pQueue
lQueue.add("C");
lQueue.add("C++");
lQueue.add("Java");
lQueue.add("Python");
// Printing the most linked element
System.out.println("Head value using peek function:" + lQueue.peek());
// Printing all elements
System.out.println("The queue elements:");
Iterator itr = lQueue.iterator();
while (itr.hasNext())
System.out.println(itr.next());
// Removing the top linked element (or head) and
// printing the modified lQueue
lQueue.poll();
System.out.println("After removing an element" +"with poll function:");
Iterator<String> itr2 = lQueue.iterator();
while (itr2.hasNext())
System.out.println(itr2.next());
// Removing Java
lQueue.remove("Java");
System.out.println("after removing Java with" + " remove function:");
Iterator<String> itr3 = lQueue.iterator();
while (itr3.hasNext())
System.out.println(itr3.next());
// Check if an element is present
boolean b = lQueue.contains("C");
System.out.println ( "Linked queue contains C" + "or not?: " + b);
// get objects from the queue in an array and
// print the array
Object[] arr = lQueue.toArray();
System.out.println ( "Value in array: ");
for (int i = 0; i<arr.length; i++)
System.out.println ( "Value: " + arr[i].toString()) ;
}
}
O/p :