Question: JAVA Implement a priority queue (priorities will be integers) using a doubly linked list. Assume that the smaller the integer the higher priority. The main
JAVA Implement a priority queue (priorities will be integers) using a doubly linked list. Assume that the smaller the integer the higher priority. The main operations of the priority queue are:
add (n): inserts priority n. max: returns (but does not delete) the highest priority. removeMax(): removes a highest priority.
Write a tester class and name it Main.
_________________________________________________________
PRIORITY QUEUE CLASS
public class PriorityQueue {
public class Node {
private int info; private Node next; private Node previous;
public Node() { info = 0; next = null; previous = null; }
public void setInfo(int i) { info = i; }
public void setNext(Node l) { next = l; }
public void setPrevious(Node j) { previous = j; }
public int getInfo() { return info; }
public Node getNext() { return next; }
public Node getPrevious() { return previous; } }
private Node first; private Node last;
public PriorityQueue() { first = new Node(); last = new Node(); first.setNext(last); last.setPrevious(first); }
public boolean isEmpty() { return (first.getNext() == last); }
public void display() { Node current = first.getNext();
while (current != last) { System.out.print(current.getInfo() + " "); current = current.getNext(); }
System.out.println(); }
public void add(int n) { //FILL IN } public int max() { //FILL IN } public void removeMax() { //FILL IN }
public int getLength() {
int length = 0; Node current = first.getNext(); while (current != null) { length++;
current = current.getNext(); } return length; }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
