Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

SimpleQueue.java - Java Reference: https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html import java.util.Queue; import java.util.NoSuchElementException; //Class for a simple queue based on SimpleList //The front of the queue should be the

SimpleQueue.java- Java Reference: https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html

import java.util.Queue;

import java.util.NoSuchElementException;

//Class for a simple queue based on SimpleList

//The "front" of the queue should be the front of

//the list.

//READ THE DOCUMENTATION for the Queue interface for more information

//on how these methods should work and when exceptions should

//be thrown. This data structure does not allow adding null.

//DO NOT re-implement nodes in this class... remember how

//inheritance works in Java and make use of that!

class SimpleQueue extends SimpleList implements Queue {

public boolean add(T item) {

//O(1)

return false;

}

public boolean offer(T item) {

//O(1)

return false;

}

public T remove() {

//O(1)

return null;

}

public T poll() {

//O(1)

return null;

}

public T element() {

//O(1)

return null;

}

public T peek() {

//O(1)

return null;

}

//-------------------------------------------------------------

// Main Method For Your Testing -- Edit all you want

//-------------------------------------------------------------

public static void main(String[] args){

SimpleQueue nums = new SimpleQueue<>();

nums.offer(2);

nums.offer(3);

nums.offer(5);

if (nums.peek() == 2 && nums.size()==3){

System.out.println("Yay 1");

}

if (nums.poll() == 2 && nums.poll() == 3

&& nums.poll() == 5 && nums.poll() == null){

System.out.println("Yay 2");

}

}

}

Since our priority queue is built out of a linked list, youll need to 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).

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Ai And The Lottery Defying Odds With Intelligent Prediction

Authors: Gary Covella Ph D

1st Edition

B0CND1ZB98, 979-8223302568

More Books

Students also viewed these Databases questions

Question

Stages of a Relationship?

Answered: 1 week ago