Question

I have a queue and I want to push into this one using the SJF algorithm also called SPN (shortest process next) I use a function to estimate the "job". The elements I want to push are structs, I get the "job" using the parameters of that struct. For example:

typedef struct {
 int* a;
 int* b;
}element;

getJob(element*){
int job = element->a * element->b;
return job;
}

It doesn't matter how is defined that function, I want to know how to implement SJF or SPN algorithm using it.

Was it helpful?

Solution

I think that a priority queue is what you're looking for. Here there are several implementation, also for c and with a little of modifications you can use for your purpose.

This data structure allow only two operation

  1. insert an element
  2. extract that one with highest priority

In your case the priority is higher, the lower the job.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top