Add comments and change implementation slightly

This commit is contained in:
Isaac Shoebottom 2023-12-02 21:30:51 -04:00
parent 4494f47a32
commit 7d4ecdaba6
2 changed files with 10 additions and 10 deletions

View File

@ -36,7 +36,7 @@ void enqueue(DiskQueue *queue, int time, int position) {
queue->size++;
}
DiskRequest *delete(DiskQueue *queue, DiskRequest request) {
void delete(DiskQueue *queue, DiskRequest request) {
DiskRequest *current = queue->head;
DiskRequest *previous = NULL;
while (current != NULL) {
@ -47,7 +47,7 @@ DiskRequest *delete(DiskQueue *queue, DiskRequest request) {
previous->next = current->next;
}
queue->size--;
return current;
return;
}
previous = current;
current = current->next;
@ -82,14 +82,14 @@ DiskRequest* findClosest(DiskQueue *queue, int position, double time) {
DiskRequest *current = queue->head;
DiskRequest *closest = NULL;
while (current != NULL) {
if (current->time <= time) {
if (closest == 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;
} else {
int closestDistance = closest->position - position;
int currentDistance = current->position - position;
if (currentDistance > 0) {
if (currentDistance < closestDistance) {
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;
}
}
@ -101,5 +101,5 @@ DiskRequest* findClosest(DiskQueue *queue, int position, double time) {
if (closest == NULL) {
return NULL;
}
return delete(queue, *closest);
return closest;
}

View File

@ -19,7 +19,7 @@ DiskQueue *createDiskQueue();
void enqueue(DiskQueue *queue, int time, int position);
DiskRequest *delete(DiskQueue *queue, DiskRequest request);
void delete(DiskQueue *queue, DiskRequest request);
DiskRequest peek(DiskQueue *queue);