Implement a priority queue capable of holding objects of an arbitrary type, T, by defining a PriorityQueue class that implements the queue with an ArrayList.A priority queue is a type of list where every item added to the queue also has an associated priority. Define priority in your application so that those items with the largest numerical value have the highest priority. Your class should support the following methods: . add (item, priority) –Adds a new item to the queue with the associated priority. remove () –Returns the item with the highest priority and removes it from the queue. If the user attempts to remove from an empty queue, return null. · tostring()-Returns a String representation of the priority queue. For example, if q is a priority queue defined to take strings q.add ("X", 10); q.add ("Y", 1); q.add ("Z", 3); System.out.println(q); // prints [ ["X",10], ["Y,1], ["Z",3] ] System.out.println(q.remove ()); // Returns X System.out.println (q.remove ()); // Returns Z System.out.println(q.remove ()) ; // Returns Y

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
Question

I need some help with this problem, more specifically, I don't really understand how to implement the actual array list and instance declaration such that the user can do things like this

PriorityQueue<Integer> queue1 = new PriorityQueue<>();

---

PriorityQueue<String> queue2 = new PriorityQueue<>();

Implement a priority queue capable of holding objects of an arbitrary type, T, by defining a PriorityQueue class that
implements the queue with an ArrayList.A priority queue is a type of list where every item added to the queue also
has an associated priority. Define priority in your application so that those items with the largest numerical value have
the highest priority. Your class should support the following methods:
. add (item, priority) –Adds a new item to the queue with the associated priority.
remove () -Returns the item with the highest priority and removes it from the queue. If the user attempts to remove
from an empty queue, return null.
· tostring()-Returns a String representation of the priority queue.
For example, if q is a priority queue defined to take strings
q. add ("X", 10);
q. add ("Y", 1);
q. add ("Z", 3);
System.out.println (q); // prints ( ["X",10], ["Y,1], ["z",3] ]
System.out.println (q.remove ()); // Returns X
System.out.println (q.remove () ); // Returns Z
System.out.println (q.remove () ); // Returns Y
Transcribed Image Text:Implement a priority queue capable of holding objects of an arbitrary type, T, by defining a PriorityQueue class that implements the queue with an ArrayList.A priority queue is a type of list where every item added to the queue also has an associated priority. Define priority in your application so that those items with the largest numerical value have the highest priority. Your class should support the following methods: . add (item, priority) –Adds a new item to the queue with the associated priority. remove () -Returns the item with the highest priority and removes it from the queue. If the user attempts to remove from an empty queue, return null. · tostring()-Returns a String representation of the priority queue. For example, if q is a priority queue defined to take strings q. add ("X", 10); q. add ("Y", 1); q. add ("Z", 3); System.out.println (q); // prints ( ["X",10], ["Y,1], ["z",3] ] System.out.println (q.remove ()); // Returns X System.out.println (q.remove () ); // Returns Z System.out.println (q.remove () ); // Returns Y
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Arrays
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning