Compare commits
2 Commits
a8-alt-sol
...
f049e8aeff
Author | SHA1 | Date | |
---|---|---|---|
f049e8aeff | |||
025b2640fc |
@ -57,10 +57,6 @@ void delete(DiskQueue *queue, DiskRequest request) {
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
DiskRequest peek(DiskQueue *queue) {
|
|
||||||
return *queue->head;
|
|
||||||
}
|
|
||||||
|
|
||||||
DiskRequest *dequeue(DiskQueue *queue) {
|
DiskRequest *dequeue(DiskQueue *queue) {
|
||||||
DiskRequest *head = queue->head;
|
DiskRequest *head = queue->head;
|
||||||
queue->head = head->next;
|
queue->head = head->next;
|
||||||
@ -79,6 +75,14 @@ void destroyDiskQueue(DiskQueue *queue) {
|
|||||||
free(queue);
|
free(queue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int calcDistance(int position, int destination) {
|
||||||
|
if (position > destination) {
|
||||||
|
return destination + (9999 + 1 - position);
|
||||||
|
} else {
|
||||||
|
return destination - position;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
DiskRequest* findClosest(DiskQueue *queue, int position, double time) {
|
DiskRequest* findClosest(DiskQueue *queue, int position, double time) {
|
||||||
DiskRequest *current = queue->head;
|
DiskRequest *current = queue->head;
|
||||||
DiskRequest *closest = NULL;
|
DiskRequest *closest = NULL;
|
||||||
@ -87,20 +91,14 @@ DiskRequest* findClosest(DiskQueue *queue, int position, double time) {
|
|||||||
if (closest == NULL) { // If there is no closest yet, set it to the current
|
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; // Distance from the closest to the current position, negative means down (bad)
|
int closestDistance = calcDistance(position, closest->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)
|
int currentDistance = calcDistance(position, current->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
|
||||||
if (currentDistance < closestDistance) { // If the current is closer than the closest
|
closest = current;
|
||||||
closest = current;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
// Bad
|
|
||||||
if (closest == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return closest;
|
return closest;
|
||||||
}
|
}
|
@ -21,10 +21,10 @@ void enqueue(DiskQueue *queue, int time, int position);
|
|||||||
|
|
||||||
void delete(DiskQueue *queue, DiskRequest request);
|
void delete(DiskQueue *queue, DiskRequest request);
|
||||||
|
|
||||||
DiskRequest peek(DiskQueue *queue);
|
|
||||||
|
|
||||||
DiskRequest *dequeue(DiskQueue *queue);
|
DiskRequest *dequeue(DiskQueue *queue);
|
||||||
|
|
||||||
DiskRequest *findClosest(DiskQueue *queue, int position, double time);
|
DiskRequest *findClosest(DiskQueue *queue, int position, double time);
|
||||||
|
|
||||||
void destroyDiskQueue(DiskQueue *queue);
|
void destroyDiskQueue(DiskQueue *queue);
|
||||||
|
|
||||||
|
int calcDistance(int position, int destination);
|
||||||
|
@ -68,7 +68,6 @@ void cscan(int start) {
|
|||||||
if (request != NULL) {
|
if (request != NULL) {
|
||||||
delete(queue, *request);
|
delete(queue, *request);
|
||||||
}
|
}
|
||||||
//printf("Movement: %i Time:%.1lf\n", movement, seekTime);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user