Home > Back-end >  Queue data structure - simple task scheduling
Queue data structure - simple task scheduling

Time:09-26

Existing N tasks such as handling, complete each task need time for T1, T2,... (as an integer), processing machine a total of two (2), request to order these tasks assigned to deal with the two machines, the principle of distribution of which is the current machine processing of short time which is assigned to the machine (if the current two machines processing tasks at the same time, is assigned to the first machine), the chronological sequence of requests have been processed output corresponding tasks,
Tip: set up two queues, the first task queue storage allocated to the first machine, the second queue storage tasks assigned to the second machine,
Initialization function initQueue complete queue; QueueEmpty whether the queue is empty; The enQueue implementation team operation; To deQueue implementation team operation; GetHead get team head element; CreateJobs implementation task execution time input, and will be added to the corresponding task queue; DealJobs achieve the queue according to the task completion time and output task number
# include
# include
# define OK 1
# define the ERROR 0
# define TRUE 1
# define FALSE 0
# define MAXSIZE 20/initial allocation amount * * queue storage/
Typedef int the Status;
//build tasks data type
Typedef struct {
Int id;//task number
Int starttime.//task start time
Int the runtime.//tasks running time
} QElemType;
/* the order of the circular queue storage structure
By using less an element method implementation,
Queue space for MAXSIZE namely, have MAXSIZE - an element that queue full,

Typedef struct {
QElemType data [MAXSIZE];
int front; The head pointer/
Int rear;/the tail pointer, if the queue is not empty, pointing to the queue tail element to the next position */
} SqQueue;
The Status initQueue (SqQueue & amp; Q);
The Status queueEmpty SqQueue (Q);
Void the enQueue (SqQueue & amp; Q, QElemType e);
The Status to deQueue (SqQueue & amp; Q, QElemType & amp; e);
QElemType getHead SqQueue (Q);
Void createJobs (SqQueue & amp; A, SqQueue & amp; B, int jobsNum);
Void dealJobs (SqQueue & amp; A, SqQueue & amp; B);
Int main (void) {
Int jobsNum;//task number
SqQueue A, B;
InitQueue (A);
InitQueue (B);
The scanf (" % d ", & amp; JobsNum);
CreateJobs (A, B, jobsNum);
DealJobs (A, B);
return 0;
}
Q/* initialize an empty queue, head to tail pointer initial setup */
The Status initQueue (SqQueue & amp; Q) {
Q.f ront=0;
Q.r ear=0;
Return OK;
}
/* if empty queue queue Q, then return TRUE, otherwise it returns FALSE */
The Status queueEmpty (SqQueue Q) {
If (Q.f ront==Q.r ear)
return TRUE;
return FALSE;
}
/* submit only the following code */
Void the enQueue (SqQueue & amp; Q, QElemType e) {
}
The Status to deQueue (SqQueue & amp; Q, QElemType & amp; E) {
}
QElemType getHead (SqQueue Q) {
}
Void createJobs (SqQueue & amp; A, SqQueue & amp; B, int jobsNum) {
}
Void dealJobs (SqQueue & amp; A, SqQueue & amp; B) {
}
Enter
Line 1 input an integer, said have n task
Line 2 input n integers, said each task needs processing time
O
According to the order of processing is completed the output the corresponding task id (if the two machines at the same time to complete the task, the first to show that the task of machine 1)
The sample input
4
10 30 15
Sample output
1 2 3 4
  • Related