Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Below is the code for a MaxPQ ( max ordered priority queue ) that is implemented using a sorted linked list. ( The items in

Below is the code for a MaxPQ (max ordered priority queue) that is implemented using a sorted linked list. (The items in the linked list appear in sorted order from greatest to least.) However, the insert method has not been implemented. In the space below the code, write the code for the insert method so that it maintains the invariant that the items in the list appear in sorted order from greatest to least. Note, that the key field in each node implements Comparable, so it will have a compareTo method.
If you cannot answer the question for a generic Key that implements Comparable, you may answer the question assuming the keys are ints and can be compared using > and < for partial credit (at most 15 out of 20 points)
public class OrderedMaxPQ>{
private class Node {
Key key;
Node next;
public Node(Key k, Node n){
key = k;
next = n;
}
}
private Node first; // The first node in a list where the keys appear in decreasing order
public OrderedMaxPQ(){
first = null;
}
public boolean isEmpty(){
return first == null;
}
public Key delMax(){
if (isEmpty()) throw new NoSuchElementException("Priority queue underflow");
Key answer = first.key;
first = first.next;
return answer;
}
public void insert(Key k){
// WRITE YOUR CODE HERE
}
}

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

Mastering Big Data Interview 751 Comprehensive Questions And Expert Answers

Authors: Mr Bhanu Pratap Mahato

1st Edition

B0CLNT3NVD, 979-8865047216

Students also viewed these Databases questions

Question

Identify three methods for determining the breakeven point.

Answered: 1 week ago

Question

Compare between the dynamic and static representation of Queue

Answered: 1 week ago