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

Question 110 pts What are the contents of the queue bankLine after the following statements execute?...
Question 110 pts What are the contents of the queue bankLine after the following statements execute? The front of the queue in the answers is the left-most value QueueInterface<String> bankLine = newLinkedQueue<>(); bankLine .enqueue(“John”); bankLine .enqueue(“Matthew”); String next = bankLine .dequeue(); bankLine .enqueue(“Drew”); bankLine .enqueue(“Heather”); bankLine .enqueue(“David”); next = bankLine .dequeue(); John, Drew, Heather Heather, John, Drew Drew, Heather, David David, Heather, Drew Flag this Question Question 210 pts What are the contents of the deque waitingLine after the following...
What are the differences between a maximum priority queue and a minimum priority queue?
What are the differences between a maximum priority queue and a minimum priority queue?
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
Say that we want to maintain both a Queue and a Priority Queue. This means that...
Say that we want to maintain both a Queue and a Priority Queue. This means that when you do Enqueue you also add the item to the Priority Queue and when you do Dequeue you also remove the item from the Priority Queue. And vise-versa. Show how to do it. Write pseudo codes or explain in words and the running time.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT