Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete the following codes. import java.util.Comparator; /** Implementation of a priority queue by means of a sorted node list. */ public class SortedListPriorityQueue implements PriorityQueue

Complete the following codes.

import java.util.Comparator; /** Implementation of a priority queue by means of a sorted node list. */

public class SortedListPriorityQueue implements PriorityQueue { protected PositionList> entries;

protected Comparator c; protected Position> actionPos; // used by subclass /** Inner class for entries */ protected static class MyEntry implements Entry { protected K k; // key protected V v; // value public MyEntry(K key, V value) { k = key; v = value; } // methods of the Entry interface public K getKey() { return k; } public V getValue() { return v; } public String toString() { return "[" + k + "," + v + "]"; }

}

/** Creates the priority queue with the default comparator. */ public SortedListPriorityQueue () { entries = new NodePositionList>(); c = new DefaultComparator(); } /** Creates the priority queue with the given comparator. */ public SortedListPriorityQueue (Comparator comp) { entries = new NodePositionList>(); c = comp; }

public int size() {return entries.size();} public boolean isEmpty() {return entries.isEmpty();} /** Returns but does not remove an entry with minimum key. */ public Entry min () throws EmptyPriorityQueueException { // Your work is here. } /** Inserts a key-value pair and return the entry created. */ public Entry insert (K k, V v) throws InvalidKeyException { Your work is here. }

/** Determines whether a given key is valid */ protected void checkKey(K key) throws InvalidKeyException {

if (key == null) throw new InvalidKeyException("Invalid key");

}

/** Auxiliary method used for insertion. */ protected void insertEntry(Entry e) { if (entries.isEmpty()) { entries.addFirst(e); // insert into empty list actionPos = entries.first(); // insertion position } else if (c.compare(e.getKey(), entries.last().element().getKey()) > 0) { entries.addLast(e); // insert at the end of the list actionPos = entries.last(); // insertion position } else { Position> curr = entries.first(); while (c.compare(e.getKey(), curr.element().getKey())> 0) { curr = entries.next(curr); // advance toward insertion position } entries.addBefore(curr, e); actionPos = entries.prev(curr); // insertion position } } /** Removes and returns an entry with minimum key. */ public Entry removeMin() throws EmptyPriorityQueueException { // your work is here }

public static void main(String[] args) { SortedListPriorityQueue SPQ = new SortedListPriorityQueue (); int n = 10; System.out.println("Inserted entries are: "); for (int i = 0; i <= n; ++i) { int s = 100 - i; int j = i + 1; SPQ.insert (s, j); System.out.println(SPQ.insert (s, j)); } System.out.println(" Sorted entries by default are: ");

for (int i = 0; i <= n; ++i) { SPQ.removeMin(); System.out.println ( SPQ.removeMin()); }

SortedListPriorityQueue SSPQ = new SortedListPriorityQueue (); String[] SArray = {"Hello", "Study", "Chenkuan Li","John Chen", "Jenna Li"};

System.out.println(); System.out.println(" Unorted strings are: ");

for (String s: SArray) { Entry e = SSPQ.insert(s, s); System.out.println (e); } System.out.println(" Sorted strings by default are: "); for (int i = 0; i <= SArray.length - 1; ++i) { Entry e = SSPQ.removeMin(); System.out.println(e); } System.out.println();

SortedListPriorityQueue SPQC = new SortedListPriorityQueue (new integerComparator());

System.out.println("Inserted entries are: "); for (int i = 0; i <= n; ++i) {

SPQC.insert (i, i); System.out.println(SPQC.insert (i, i)); } System.out.println(" Sorted entries by the integerComparator are: ");

for (int i = 0; i <= n; ++i) { SPQC.removeMin(); System.out.println ( SPQC.removeMin()); }

System.out.println();

SortedListPriorityQueue SPQCB = new SortedListPriorityQueue (new BinaryTest());

System.out.println("Inserted entries are: "); for (int i = 0; i <= n; ++i) {

SPQCB.insert (i, i); System.out.println(SPQCB.insert (i, i)); } System.out.println(" Sorted entries by the BinaryTest are: ");

for (int i = 0; i <= n; ++i) { SPQCB.removeMin(); System.out.println ( SPQCB.removeMin()); }

System.out.println();

} }

Please who can help with this question in Java

Thank you

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

Modern Database Management

Authors: Heikki Topi, Jeffrey A Hoffer, Ramesh Venkataraman

13th Edition

0134773659, 978-0134773650

More Books

Students also viewed these Databases questions