Add comments and change implementation slightly
This commit is contained in:
parent
4494f47a32
commit
7d4ecdaba6
@ -36,7 +36,7 @@ void enqueue(DiskQueue *queue, int time, int position) {
|
|||||||
queue->size++;
|
queue->size++;
|
||||||
}
|
}
|
||||||
|
|
||||||
DiskRequest *delete(DiskQueue *queue, DiskRequest request) {
|
void delete(DiskQueue *queue, DiskRequest request) {
|
||||||
DiskRequest *current = queue->head;
|
DiskRequest *current = queue->head;
|
||||||
DiskRequest *previous = NULL;
|
DiskRequest *previous = NULL;
|
||||||
while (current != NULL) {
|
while (current != NULL) {
|
||||||
@ -47,7 +47,7 @@ DiskRequest *delete(DiskQueue *queue, DiskRequest request) {
|
|||||||
previous->next = current->next;
|
previous->next = current->next;
|
||||||
}
|
}
|
||||||
queue->size--;
|
queue->size--;
|
||||||
return current;
|
return;
|
||||||
}
|
}
|
||||||
previous = current;
|
previous = current;
|
||||||
current = current->next;
|
current = current->next;
|
||||||
@ -82,14 +82,14 @@ DiskRequest* findClosest(DiskQueue *queue, int position, double time) {
|
|||||||
DiskRequest *current = queue->head;
|
DiskRequest *current = queue->head;
|
||||||
DiskRequest *closest = NULL;
|
DiskRequest *closest = NULL;
|
||||||
while (current != NULL) {
|
while (current != NULL) {
|
||||||
if (current->time <= time) {
|
if (current->time <= time) { // Filter out requests that haven't come in yet
|
||||||
if (closest == NULL) {
|
if (closest == NULL) { // If there is no closest yet, set it to the current
|
||||||
closest = current;
|
closest = current;
|
||||||
} else {
|
} else {
|
||||||
int closestDistance = closest->position - position;
|
int closestDistance = closest->position - position; // Distance from the closest to the current position, negative means down (bad)
|
||||||
int currentDistance = current->position - position;
|
int currentDistance = current->position - position; // Distance from the current to the current position, negative means down (bad)
|
||||||
if (currentDistance > 0) {
|
if (currentDistance > 0) { // If the current is up
|
||||||
if (currentDistance < closestDistance) {
|
if (currentDistance < closestDistance) { // If the current is closer than the closest
|
||||||
closest = current;
|
closest = current;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -101,5 +101,5 @@ DiskRequest* findClosest(DiskQueue *queue, int position, double time) {
|
|||||||
if (closest == NULL) {
|
if (closest == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return delete(queue, *closest);
|
return closest;
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ DiskQueue *createDiskQueue();
|
|||||||
|
|
||||||
void enqueue(DiskQueue *queue, int time, int position);
|
void enqueue(DiskQueue *queue, int time, int position);
|
||||||
|
|
||||||
DiskRequest *delete(DiskQueue *queue, DiskRequest request);
|
void delete(DiskQueue *queue, DiskRequest request);
|
||||||
|
|
||||||
DiskRequest peek(DiskQueue *queue);
|
DiskRequest peek(DiskQueue *queue);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user