Question

In: Computer Science

A priority queue is a special queue that adjusts the location of items based on some...

  1. A priority queue is a special queue that adjusts the location of items based on some priority value. When inserted, a new value is placed in the queue ahead of every other item that has a lower priority than it. This gives us a queue that removes items from the front removing highest priority first, and from items with similar priority the ones that were inserted first. For example, if we inserted the following items with the priority {A, 1}, {B,2}, {C,2}, {D,3}, {E,1} where the lower number is a lower priority, our queue will have the items from front to back: D,B,C,A,E. Using the partial code below, you are to implement the Enqueue method so that the new node is inserted into the appropriate place in the queue based on the priority. (Assume that a higher number is higher priority, for example 0 is lowest and 10 is highest)

class Node<V,P>

{

      public V value;

      public P priority;

      public Node<V,P> next;

     

      Node(V value, P priority)

      {

           this.value = value;

           this.priority = priority;

      }

}

class PriorityQueue<V,P>

{

      private Node<V,P> head;

      private Node<V,P> tail;

     

      ...

      public void Enqueue(V value, P priority)

      {

Solutions

Expert Solution

1). ANSWER :

GIVENTHAT :

The priority is always an integer in our case. If we make even the priority generic, there is no way to compare two generic types so we type cast the priority.

public class PriorityQueue<V,P> {

    private Node<V,P> head;
    private Node<V,P> tail;

    public void Enqueue(V value, P priority){
        // Make a new node with given values.
        Node<V,P> newNode = new Node<>(value, priority);

        // If head is null, make the newNode as head and return.
        if(head == null){
            head = newNode;
            tail = newNode;
            return;
        }

        Node<V,P> cur = head;

        // Make integers from priority as we cannot compare generic types.
        // Please ensure that the priority is a number.
        Integer priority1 = (Integer) (priority);
        Integer priorityHead = (Integer)(head.priority);

        // Check if priority of newNode is greater than priority of head,
        // If it is insert at beginning and update value of head.
        if(priority1 > priorityHead){
            newNode.next = head;
            head = newNode; 
            return;
        }


        // Find the position of newNode in the queue and insert there.
       while(cur != null && cur.next != null){
           
           Integer priority2 = (Integer) (cur.next.priority);

           if(priority1 > priority2){
                Node<V,P> Next = cur.next;
                cur.next = newNode;
                newNode.next = Next;
                return;
           }
           else{
               cur = cur.next;
           }
       }

       // If no appropriate position is found in the middle of queue, insert at end.
       tail.next = newNode;
       tail = newNode;
    }

    // Utility function to print contents of queue.
    void print(){
        Node<V,P> cur = head;
        while(cur != null){
            System.out.println(cur.value + " " + cur.priority);
            cur = cur.next;
        }
        System.out.println("");
    }
    // Driver method to test
    public static void main(String[] args){
        PriorityQueue<Integer,Integer> pq = new PriorityQueue<>();

        pq.Enqueue(1, 5);
        
        pq.Enqueue(100, 0);
        pq.Enqueue(5, 10);
        pq.Enqueue(101, 3);
        pq.Enqueue(5, -1);
        pq.Enqueue(50, 100);
        pq.Enqueue(32, 50);
        pq.print();

    }
    
}

The above program Output : -

As we can see the new entries are inserted in order of priority.


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
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?
// 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...
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...
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. You will then attach priorityInsert(long key) and priorityRemove() methods). AND Use the provided PQDoublyLinkedTest.java to test your code. BOTH CODES SHOULD WORK TOGETHER, YOU JUST HAVE TO ADD priorityInsert(int). PLEASE PROVIDE...
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), and a driver...
#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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT