diff --git a/Assignment8/code/disk.c b/Assignment8/code/disk.c index 03ae242..b15d1a5 100644 --- a/Assignment8/code/disk.c +++ b/Assignment8/code/disk.c @@ -79,6 +79,14 @@ void destroyDiskQueue(DiskQueue *queue) { free(queue); } +int calcDistance(int position, int destination) { + if (position > destination) { + return destination + (10000 - position); + } else { + return destination - position; + } +} + DiskRequest* findClosest(DiskQueue *queue, int position, double time) { DiskRequest *current = queue->head; DiskRequest *closest = NULL; @@ -87,20 +95,14 @@ DiskRequest* findClosest(DiskQueue *queue, int position, double time) { if (closest == NULL) { // If there is no closest yet, set it to the current closest = 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; - } + int closestDistance = calcDistance(position, closest->position); // Distance from the closest to the current position, negative means down (bad) + int currentDistance = calcDistance(position, current->position); // Distance from the current to the current position, negative means down (bad + if (currentDistance < closestDistance) { // If the current is closer than the closest + closest = current; } } } current = current->next; } - // Bad - if (closest == NULL) { - return NULL; - } return closest; -} +} \ No newline at end of file diff --git a/Assignment8/code/disk.h b/Assignment8/code/disk.h index ae379de..f83bd66 100644 --- a/Assignment8/code/disk.h +++ b/Assignment8/code/disk.h @@ -28,3 +28,5 @@ DiskRequest *dequeue(DiskQueue *queue); DiskRequest *findClosest(DiskQueue *queue, int position, double time); void destroyDiskQueue(DiskQueue *queue); + +int calcDistance(int position, int destination);