Question

In: Computer Science

As the following statements execute, what is the content of the priority queue? Show the content...

As the following statements execute, what is the content of the priority queue? Show the content of the queue after each step:

PriorityQueue<String> myPriorityQueue = new PriorityQueue<>();

          


myPriorityQueue.offer("Jim");

          


myPriorityQueue.offer ("Jess");

          


myPriorityQueue.offer ("Jill");

          


myPriorityQueue.offer ("Jane");

          


String name = myPriorityQueue.poll();

          


myPriorityQueue.offer (name);

          


myPriorityQueue.offer (myPriorityQueue.peek());

          


myPriorityQueue.offer ("Jim");

          


myPriorityQueue.poll();

Solutions

Expert Solution

Here,
offer() is just like an add operation to the Priority queue.(By default adds in ascending order)
peek() will just return the head from the Priority queue.
poll() will return the head and removed from the Priority queue.

i.) PriorityQueue<String> myPriorityQueue = new PriorityQueue<>();

Here myPriorityQueue will be created

ii.) myPriorityQueue.offer("Jim");

Jim will be added to myPriorityQueue.

myPriorityQueue=["Jim"]

iii.) myPriorityQueue.offer ("Jess");

Jess will be added to myPriorityQueue.

myPriorityQueue=["Jess","Jim"]

iv.) myPriorityQueue.offer ("Jill");

Jill will be added to myPriorityQueue.

myPriorityQueue=["Jess","Jill","Jim"]

v.) myPriorityQueue.offer ("Jane");

Jane will be added to myPriorityQueue.

myPriorityQueue=["Jane","Jess","Jill","Jim"]

vi.) String name = myPriorityQueue.poll();

The head of myPriorityQueue will be returned and removed from it. So here, Jane will be returned and removed from the queue.

name="Jane"

myPriorityQueue=["Jess","Jill","Jim"]

vii.) myPriorityQueue.offer (name);

Here name="Jane"

Jane will be added to myPriorityQueue.

myPriorityQueue=["Jane","Jess","Jill","Jim"]

viii.) myPriorityQueue.offer (myPriorityQueue.peek());

peek() will just return the head

myPriorityQueue.peek() wil give us "Jane"

myPriorityQueue.offer (myPriorityQueue.peek()) will add "Jane" to the queue

myPriorityQueue=["Jane","Jane","Jess","Jill","Jim"]

ix.)myPriorityQueue.offer ("Jim");

Jim will be added to myPriorityQueue.

myPriorityQueue=["Jane","Jane","Jess","Jill","Jim","Jim"]

x.) myPriorityQueue.poll();

The head will be returned and removed from the queue. So, "Jane" will be returned and removed from the queue

myPriorityQueue=["Jane","Jess","Jill","Jim","Jim"]

So, after executing the above operations myPriorityQueue=["Jane","Jess","Jill","Jim","Jim"]

#Please don't forget to upvote if you find the solution helpful. Feel free to ask doubts if any, in the comments section. Thank you.


Related Solutions

Priority Queue Application: Use your Java's Priority Queue. Make a priority queue to represent customers being...
Priority Queue Application: Use your Java's Priority Queue. Make a priority queue to represent customers being served at the Department of Motor Vehicles. Start with 100 customers in a List. In a loop, generate a priority for each customer (1-5) In a second loop, add the users to a priority queue Print the List and the Priority Queue
Use a priority queue to simulate prioritized jobs Priority Queue class Queue Class Node Class (Node...
Use a priority queue to simulate prioritized jobs Priority Queue class Queue Class Node Class (Node will have a 4 digit job number and a priority (A, B, etc) with A highest priority In the driver Create a priority queue object Add 3 jobs of 'B' priority Add 4 jobs of 'D' priority Add 2 jobs of highest priority Print the queue
Implement a priority queue using a DoublyLinkedList where the node with the highest priority (key) is...
Implement a priority queue using a DoublyLinkedList where the node with the highest priority (key) is the right-most node. The remove (de-queue) operation returns the node with the highest priority (key). If displayForward() displays List (first-->last) : 10 30 40 55 remove() would return the node with key 55. Demonstrate by inserting keys at random, displayForward(), call remove then displayForward() again. You will then attach a modified DoublyLinkedList.java (to contain the new priorityInsert(long key) and priorityRemove() methods). Use the provided...
((by C++ ))Write a program that will reverse the content of a Queue using the following...
((by C++ ))Write a program that will reverse the content of a Queue using the following standard queue operations. enqueue(x) : Add an item x to rear of queue. dequeue() : Remove an item from front of queue. empty() : Checks if a queue is empty or not. For reversing the queue one approach could be to store the elements of the queue in a temporary data structure in a manner such that if we re-insert the elements in the...
#include <iostream> #include <queue> //This contains the STL's efficient implementation of a priority queue using a...
#include <iostream> #include <queue> //This contains the STL's efficient implementation of a priority queue using a heap using namespace std; /* In this lab you will implement a data structure that supports 2 primary operations: - insert a new item - remove and return the smallest item A data structure that supports these two operations is called a "priority queue". There are many ways to implement a priority queue, with differernt efficiencies for the two primary operations. For this lab,...
write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
// priorityList.java // a priority queue based on a sorted list // to run this program:...
// priorityList.java // a priority queue based on a sorted list // to run this program: C>java PiorityQApp //////////////////////////////////////////////////////////////// class Link { public long dData; // data item public Link next; // next link in list // ------------------------------------------------------------- public Link(long dd) // constructor { dData = dd; } // ------------------------------------------------------------- public void displayLink() // display this link { System.out.print(dData + " "); } } // end class Link //////////////////////////////////////////////////////////////// class SortedList { private Link first; // ref to first item...
After the following statements execute, what are the contents of the deque? DequeInterface<String> myDeque = new...
After the following statements execute, what are the contents of the deque? DequeInterface<String> myDeque = new LinkedDeque<>(); myDeque.addToFront("Jim"); myDeque.addToFront("Jess"); myDeque.addToBack("Jill"); myDeque.addToBack("Jane"); String name = myDeque.removeFront(); myDeque.addToBack(name); myDeque.addToBack(myDeque.getFront()); myDeque.addToFront(myDeque.removeBack()); myDeque.addToFront(myDeque.getBack()); I need to get this as an output: Jim Jess Jim Jess Jim Jill Jess Jim Jill Jane Jim Jill Jane Jim Jill Jane Jess Jim Jill Jane Jess Jim Jim Jim Jill Jane Jess Jess Jim Jim Jane Jess Help me please
Assume you have the following jobs to execute with one processor: i t(pi) Priority 0 80...
Assume you have the following jobs to execute with one processor: i t(pi) Priority 0 80 2 1 25 4 2 15 3 3 20 4 4 45 1 The jobs are assumed to arrive at the same time. Using priority scheduling followed by FCFS, do the following: Create a Gantt chart illustrating the execution of these processes. What is the turnaround time for process p1? What is the average wait time for the processes?
Write a C program to implement the priority queue with the operations insert and extractmax. Sample...
Write a C program to implement the priority queue with the operations insert and extractmax. Sample : ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 2 enter any key to go to main menu ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 4 enter any key to go to main menu ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 6 enter any key to go to main menu...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT