In: Computer Science
Use a priority queue to simulate prioritized jobs
// Java
// if you have any problem let me know i will try to help you. Thank you.
import java.util.Comparator;
import java.util.PriorityQueue;
class Node implements Comparator<Node>{
int jobNumber;
char priority;
Node(int n,char prio)
{
jobNumber=n;
priority=prio;
}
Node()
{
}
@Override
public int compare(Node o1, Node o2) {
return o1.priority-o2.priority;
}
}
public class Main {
public static void main(String[] args) {
PriorityQueue<Node> pq=new PriorityQueue<Node>(new Node());
// Add 3 jobs of B priority
pq.add(new Node(1234,'B'));
pq.add(new Node(1854,'B'));
pq.add(new Node(1658,'B'));
// 4 jobs of D priority
pq.add(new Node(1584,'D'));
pq.add(new Node(1934,'D'));
pq.add(new Node(2534,'D'));
pq.add(new Node(8234,'D'));
// Add 2 jobs of highest priority
pq.add(new Node(7854,'A'));
pq.add(new Node(9854,'A'));
//printing the queue
while(!pq.isEmpty())
{
Node temp=pq.poll();
System.out.println(temp.jobNumber+" "+temp.priority);
}
}
}
// Sample output:-