diff --git a/Assignment8/code/disk.c b/Assignment8/code/disk.c index 03ae242..ea9d56c 100644 --- a/Assignment8/code/disk.c +++ b/Assignment8/code/disk.c @@ -81,26 +81,34 @@ void destroyDiskQueue(DiskQueue *queue) { DiskRequest* findClosest(DiskQueue *queue, int position, double time) { DiskRequest *current = queue->head; - DiskRequest *closest = NULL; + DiskRequest *next = NULL; + DiskRequest *first = NULL; while (current != NULL) { if (current->time <= time) { // Filter out requests that haven't come in yet - if (closest == NULL) { // If there is no closest yet, set it to the current - closest = current; + if (first == NULL) { + first = current; } else { - int closestDistance = closest->position - position; // Distance from the closest to the current position, negative means down (bad) - int currentDistance = current->position - position; // Distance from the current to the current position, negative means down (bad) - if (currentDistance > 0) { // If the current is up - if (currentDistance < closestDistance) { // If the current is closer than the closest - closest = current; + if (current->position < first->position) { + first = current; + } + } + int currentDistance = current->position - position; // Distance from the current to the current position, negative means down (bad) + if (currentDistance > 0) { // If the current is up + if (next == NULL) { // If there is no next yet, set it to the current + next = current; + } else { + int nextDistance = next->position - position; // Distance from the next to the current position, negative means down (bad) + if (currentDistance < nextDistance) { // If the current is closer than the next + next = current; } } } } current = current->next; } - // Bad - if (closest == NULL) { - return NULL; + // If we didn't find a request that is above the current position, return the first as a fallback + if (next == NULL) { + return first; } - return closest; + return next; } diff --git a/Assignment8/code/main.c b/Assignment8/code/main.c index 826b776..123bd39 100644 --- a/Assignment8/code/main.c +++ b/Assignment8/code/main.c @@ -93,4 +93,4 @@ int main(int argc, char **argv) { destroyDiskQueue(queue); return 0; -} \ No newline at end of file +}