Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 8 (17 points): A priority queue is a queue where a numeric priority is

ID: 3880822 • Letter: Q

Question

Question 8 (17 points): A priority queue is a queue where a numeric priority is associated with each element Access to elements that have been inserted into the queue is limited to inspection and removal of the elements with smallest and largest priority only. A priority queue may have multiple items that are of equal priority Give the ADT specification for a bounded priority queue using the specification method described in Topic 7 of the lecture notes. By "bounded", it is meant that the priority queue has a maximum capacity specified when it is created, and it can never contain more than that number of items. Your specification must specify the following operations: Page 3 newPriorityQueue: make a new queue insert: inserts an element with a certain priority isEmpty: test if the queue is empty isFull: test if the queue is ful maxltem: obtain the item in the queue with the highest priority minltem: obtain the item in the queue with the lowest priority deleteMax: remove from the queue the item with the highest priority deleteAllMax: remove from the queue all items that are tied for the highest priority deleteMin: remove from the queue the item with the lowest priority frequency: obtain the number of times a certain item occurs in the queue (with any priority)

Explanation / Answer

Hii... I have implemented the priority queue. Please check below code.

PQueue.java

import java.util.*;

class PQueue

{

public static void main(String args[])

{

// Creating empty priority queue

PriorityQueue<Integer> pQueue =

new PriorityQueue<Integer>(4);

  

// test if queue is empty

if(pQueue.isEmpty()){

System.out.println("Queue is empty");

}

// Adding items to the pQueue

System.out.println("Adding queue elements");

pQueue.add(4);

pQueue.add(2);

pQueue.add(3);

pQueue.add(1);

System.out.println("The queue elements:");

Iterator itr = pQueue.iterator();

while (itr.hasNext())

System.out.println(itr.next());

  

// test if queue is empty

if(pQueue.isEmpty()){

System.out.println("Queue is empty");

}

  

// test if queue is full

if(pQueue.size()==4){

System.out.println("Queue is full");

}

System.out.println("Min item using peek function:"

+ pQueue.peek());

  

Object[] arr = pQueue.toArray();

int max = (int) arr[0];

for (int i = 0; i<arr.length; i++){

if(max < (int)arr[i])

max=(int)arr[i];

}

System.out.println ( "Max item using peek function:" + max) ;

// remove min value  

pQueue.poll();

System.out.println("The queue elements:");

itr = pQueue.iterator();

while (itr.hasNext())

System.out.println(itr.next());

// remove max element

pQueue.remove(max);

System.out.println("The queue elements:");

itr = pQueue.iterator();

while (itr.hasNext())

System.out.println(itr.next());

  

System.out.println("2 occurs in queue was "+Collections.frequency(pQueue, 2));

  

}

}

Output:

Queue is empty
Adding queue elements
The queue elements:
1
2
3
4
Queue is full
Min item using peek function:1
Max item using peek function:4
The queue elements:
2
4
3
The queue elements:
2
3
2 occurs in queue was 1

Please check the code and let me know any issues. Thank you. All the best.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote