Question

In: Computer Science

Write a java class for a Priority Queue. Use an arraylist, and include enque, deque, and...

Write a java class for a Priority Queue. Use an arraylist, and include enque, deque, and a method to get all the values of the queue. (This is not writing a file implementing the java class PriorityQueue, but rather you are writing a program that is a priority queue).

Solutions

Expert Solution

COde:

//PriorityQueue.java

import java.util.*;
public class PriorityQueue
{

   private ArrayList queueArray;
   private ArrayList priorityArray;
  
   //Constructor
   public PriorityQueue()
   {
       queueArray=new ArrayList();
       priorityArray=new ArrayList();
   }
   //Adds the items in the queue list by using priority
   public <T> void add(T itemAdd,int priorityAdd)
   {
       queueArray.add(itemAdd);
       priorityArray.add(priorityAdd);
      
   }
   public boolean isEmpty()
   {
       return queueArray.size()==0;
      
   }

   public <T> T remove()
   {
       int max=0,maxPriority=0;
       for(int i=0;i<queueArray.size();i++)
       {
           int priority = ((Integer) priorityArray.get(i)).intValue();
           if( priority> maxPriority)
           {
               max=i;
               maxPriority=priority;
           }
       }
       priorityArray.remove(max);
       return (T) queueArray.remove(max);
   }
  
   public void Print()
   {
       for(int i=0;i<queueArray.size();i++)
       {
           System.out.println(queueArray.get(i));
       }
   }
  
}

//PriorityDemo.java

public class PriorityDemo
{
   public static void main(String args[])
   {
       //Define PriorityQueue's class object is q
       PriorityQueue q=new PriorityQueue();
      
       //Input values
       System.out.println("Queue Value:");
       q.add("X", 10);
       q.add("Y", 1);
       q.add("Z", 3);
       //printing Queue
       q.Print();
         
       //Removing value
       System.out.println("Queue Value after remove:");
       q.remove();
       q.Print();
   }
}

Output:


Related Solutions

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
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
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
#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,...
A Java question. Write the class Staff. It contains methods that manipulate an ArrayList of Strings...
A Java question. Write the class Staff. It contains methods that manipulate an ArrayList of Strings representing the names of staff members. The constructor takes an ArrayList of String names as a parameter. In addition to the constructor, you need to implement the following methods The methods 1. public boolean equals(Staff other) - Determines if the other Staff contains all the same elements in the same order as this Staff 2. public boolean sameContents(Staff other) - Determines if the other...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
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?
How to write a max-heap implementation of a templated priority queue class using the STL std::vector...
How to write a max-heap implementation of a templated priority queue class using the STL std::vector class to hold the data array
write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
Implement the minimum priority queue UnsortedMPQ (using vector) that is a child class of the provided...
Implement the minimum priority queue UnsortedMPQ (using vector) that is a child class of the provided MPQ class. The functions from MPQ that are virtual function (remove min(), is empty(), min(), and insert()) must be implemented in the child classes. The functions remove min() and min() should throw an exception if the minimum priority queue is empty. For the UnsortedMPQ class, you will use a vector to implement the minimum priority queue functions. The insert() function should be O(1) and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT