Question

Hi how does this code work'?njobs is the number of jobs in queue?? how can we add a job in queue in priority order?// add a new job to the queue in priority order void addToQ(JobQ *g, Job j) assert( != NULL); int i, ki for (i = 0; i < q->nj

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The addToQ method adds Job j to the Job Queue q keeping in mind the priority of the job.

  1. Firstly, the assert function checks whether the job is empty of not.
  2. If the job queue is not empty, the for loop iterates to an index "i" in the job queue until it finds a job whose priority is less than the priority of the job to be added. Also, this is the index where the new job is to be added since all the elements in the queue starting from this index i.e (i) till the end have less priority than the new job.
  3. However, to add this new job we need to create space at the index "i". The second for loop shifts all the elements whose priority is less than the priority of the job to be inserted, one index right so as to create an empty block for the new job ''j" at index "i".
  4. Finally, the new job is inserted at the position "i" in the job queue using "q->jobs[i]=j" command.
  5. Since a new job is added to the queue now, the variable keeping a count of no of jobs in the queue needs to be incremented which is accomplished through the command "q->njobs++".

Thanks. If you like the answer, please upvote.

Add a comment
Know the answer?
Add Answer to:
Hi how does this code work'?njobs is the number of jobs in queue?? how can we...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CSC 212 Page 6 of 9 Question 4..... ........... 20 points We can represent the path...

    CSC 212 Page 6 of 9 Question 4..... ........... 20 points We can represent the path from the root to any node in a non empty binary tree using a string containing! the letters 'L' and 'R'. The letter 'L' indicates going left, whereas 'R' indicates going right. Example 3. In this tree, the empty string corte- sponds to the root 'A', "LL"corresponds to 'D', "LR" corresponds to 'E' and "RR" corresponds to 'F'. -O- DE (a) Write the method...

  • Sort a queue in java with generic type, below is my approach which has some issues....

    Sort a queue in java with generic type, below is my approach which has some issues. The porpose is to implement a method to sort the elements of the queue in ascending order, but I am not sure how to deal with generic type T instead of int or string. Need your help to solve this problem. Please post the correct code and the idea, thanks! public static <T extends Comparable<T>> int minval(Queue<T> queue, int sort) { int min_index =...

  • Design and implement a class Q that uses Q.java as a code base. The queue ADT...

    Design and implement a class Q that uses Q.java as a code base. The queue ADT must use class LinkedList from Oracle's Java class library and its underlying data structure (i.e. every Q object has-a (contains) class LinkedList object. class Q is not allowed to extend class LinkedList. The methods that are to be implemented are documented in Q.java. Method comment blocks are used to document the functionality of the class Q instance methods. The output of your program must...

  • package algs24; import stdlib.StdIn; import stdlib.StdOut; /** * The <tt>PtrHeap</tt> class is the priorityQ class from...

    package algs24; import stdlib.StdIn; import stdlib.StdOut; /** * The <tt>PtrHeap</tt> class is the priorityQ class from Question 2.4.24. * It represents a priority queue of generic keys. *   * It supports the usual <em>insert</em> and <em>delete-the-maximum</em> * operations, along with methods for peeking at the maximum key, * testing if the priority queue is empty, and iterating through * the keys. * For additional documentation, see <a href="http://algs4.cs.princeton.edu/24pq">Section 2.4</a> of * <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne....

  • Hello! I have a problem in my code please I need help, I don't know How I can wright precondition, so I need help about assertion of pre_condition of peek. Java OOP Task is! Improve the circular a...

    Hello! I have a problem in my code please I need help, I don't know How I can wright precondition, so I need help about assertion of pre_condition of peek. Java OOP Task is! Improve the circular array implementation of the bounded queue by growing the elements array when the queue is full. Add assertions to check all preconditions of the methods of the bounded queue implementation. My code is! public class MessageQueue{ public MessageQueue(int capacity){ elements = new Message[capacity];...

  • In the code shown above are two parts of two different exercises. HOW WE CAN HAVE...

    In the code shown above are two parts of two different exercises. HOW WE CAN HAVE BOTH OF THEM ON THE SAME CLASS BUT SO WE CAN RECALL them threw different methods. Thank you. 1-First exercise import java.util.Scanner; public class first { public static int average(int[] array){    int total = 0;    for(int x: array)        total += x;    return total / array.length;    } public static double average(double[] array){    double total = 0;    for(double x: array)        total += x;    return total /...

  • JAVA Have not gotten the public Iterator<Item> iterator() . Can someone explain it please? import java.util.Iterator;...

    JAVA Have not gotten the public Iterator<Item> iterator() . Can someone explain it please? import java.util.Iterator; /* * GroupsQueue class supporting addition and removal of items * with respect to a given number of priorities and with * respect to the FIFO (first-in first-out) order for items * with the same priority. * * An example, where GroupsQueue would be useful is the airline * boarding process: every passenger gets assigned a priority, * usually a number, e.g., group 1,...

  • java  Operating System Scheduler Two scheduling strategies for an operating system scheduler are first come first serve...

    java  Operating System Scheduler Two scheduling strategies for an operating system scheduler are first come first serve (FCFS) and fixed priority pre-emptive scheduling (FPPS). Since queues operate on a first come first serve basis, FCFS is implemented using a queue. Similarly, FPPS is implemented using a priority queue. The operating system scheduler simulation is already provided for you. To use it you simply need to modify the main method to run the simulator using either the LinkedListQueue or the PriorityQueue. Run...

  • Code in C++. Can someone make it so that the code below can be compiled? ▪...

    Code in C++. Can someone make it so that the code below can be compiled? ▪ Creating an empty queue ▪ Inserting a value ▪ Removing a value ▪ Finding the size of the queue ▪ Printing the contents of the queue ▪ Adding the contents of one queue to the end of another ▪ Merging the contents of two queues into a third, new, queue Class Attributes Your class should be implemented using a linked list and should have...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT