Question
Implement an update() method which accepts an item and updates its place in the queue (i.e. update its priority). Lower priority items should be at
Implement an update() method which accepts an item and "updates" its place in the queue (i.e. update its priority). Lower priority items should be at the front of the queue (since well want to pick the shortest lines, not the longest lines in our grocery store).
https://www.chegg.com/homework-help/questions-and-answers/import-javautilqueue-import-javautilnosuchelementexception-class-simple-queue-based-simple-q34996504?trackid=AkMAJs02
import java.util.NoSuchElementException; import java.util.Date; //for testing
//priority queue where the minimum item has the highest priority class PriorityQueue> extends SimpleQueue { //updates an item that's already in the queue //NOTE: This should update the exact item in memory, //not just any "equal" item (in other words, you //should use == here and not .equals())
public void update(T item) { //O(n) //throws NoSuchElementException if item is not //in the queue } //You may need to override some other methods from SimpleQueue! //Restriction 1: all methods from SimpleQueue should still work //(as in, if you add(), the value should be added, if you call //size() it should return the correct value, etc.). However, //remove/poll will remove the _minimum_ value from the queue; //element/peek will return the _minimum_ value from the queue. //Restriction 2: element() and peek() must still both be O(1) //------------------------------------------------------------- // Main Method For Your Testing -- Edit all you want //------------------------------------------------------------- public static void main(String[] args){ PriorityQueue values = new PriorityQueue<>(); Date[] dates = new Date[5]; for (int i=5; i>=1; i--){ dates[i-1] = new Date(86400000*i); values.add(dates[i-1]); } for(Date d : values) { System.out.println(d); }
dates[3].setTime(0); values.update(dates[3]); System.out.println(); for(Date d : values) { System.out.println(d); } if(values.peek().equals(dates[3])) { System.out.println(" Yay 1"); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started