Move to linked list implementation
This commit is contained in:
parent
fb6ec1dc98
commit
6189cf6a3b
@ -2,91 +2,6 @@
|
||||
#include <stdio.h>
|
||||
#include "disk.h"
|
||||
|
||||
void destroyDiskQueue(DiskQueue *queue) {
|
||||
free(queue->requests);
|
||||
free(queue);
|
||||
}
|
||||
|
||||
DiskQueue *sortQueueByPosition(DiskQueue *queue) {
|
||||
qsort(queue->requests, queue->size, sizeof(DiskRequest), compareDiskRequestsByPosition);
|
||||
return queue;
|
||||
}
|
||||
|
||||
int compareDiskRequestsByPosition(const void *a, const void *b) {
|
||||
DiskRequest *requestA = (DiskRequest *) a;
|
||||
DiskRequest *requestB = (DiskRequest *) b;
|
||||
return requestA->position - requestB->position;
|
||||
}
|
||||
|
||||
DiskQueue *queueOfLesserTime(DiskQueue *queue, double time) {
|
||||
DiskQueue *sameTimeQueue = createDiskQueue();
|
||||
for (int i = 0; i < queue->size; i++) {
|
||||
if (queue->requests[i].time <= time) {
|
||||
enqueue(sameTimeQueue, queue->requests[i]);
|
||||
}
|
||||
}
|
||||
return sameTimeQueue;
|
||||
}
|
||||
|
||||
DiskRequest dequeue(DiskQueue *queue) {
|
||||
return delete(queue, peek(queue));
|
||||
}
|
||||
|
||||
DiskRequest peek(DiskQueue *queue) {
|
||||
return queue->requests[0];
|
||||
}
|
||||
|
||||
DiskQueue *clone(DiskQueue *queue) {
|
||||
DiskQueue *clone = createDiskQueue();
|
||||
for (int i = 0; i < queue->size; i++) {
|
||||
enqueue(clone, queue->requests[i]);
|
||||
}
|
||||
return clone;
|
||||
}
|
||||
|
||||
DiskRequest delete(DiskQueue *queue, DiskRequest request) {
|
||||
// Find the index of the request
|
||||
int index = -1;
|
||||
for (int i = 0; i < queue->size; i++) {
|
||||
if (queue->requests[i].position == request.position && queue->requests[i].time == request.time) {
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (index == -1) {
|
||||
printf("Error: Request not found in queue\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// Shift all the elements after the index down one
|
||||
for (int i = index; i < queue->size - 1; i++) {
|
||||
queue->requests[i] = queue->requests[i + 1];
|
||||
}
|
||||
// Resize the queue
|
||||
queue->requests = realloc(queue->requests, sizeof(DiskRequest) * (queue->size - 1));
|
||||
if (queue->requests == NULL) {
|
||||
printf("Error reallocating memory for queue requests\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
queue->size--;
|
||||
return request;
|
||||
}
|
||||
|
||||
void enqueue(DiskQueue *queue, DiskRequest request) {
|
||||
queue->requests = realloc(queue->requests, sizeof(DiskRequest) * (queue->size + 1));
|
||||
if (queue->requests == NULL) {
|
||||
printf("Error reallocating memory for queue requests\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
queue->requests[queue->size] = request;
|
||||
queue->size++;
|
||||
}
|
||||
|
||||
DiskRequest diskRequest(int position, int time) {
|
||||
DiskRequest request;
|
||||
request.position = position;
|
||||
request.time = time;
|
||||
return request;
|
||||
}
|
||||
|
||||
DiskQueue *createDiskQueue() {
|
||||
DiskQueue *queue = malloc(sizeof(DiskQueue));
|
||||
@ -94,11 +9,92 @@ DiskQueue *createDiskQueue() {
|
||||
printf("Error allocating memory for queue\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
queue->requests = malloc(sizeof(DiskRequest));
|
||||
if (queue->requests == NULL) {
|
||||
printf("Error allocating memory for queue requests\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
queue->head = NULL;
|
||||
queue->size = 0;
|
||||
return queue;
|
||||
}
|
||||
|
||||
void enqueue(DiskQueue *queue, int time, int position) {
|
||||
DiskRequest *request = malloc(sizeof(DiskRequest));
|
||||
if (request == NULL) {
|
||||
printf("Error allocating memory for request\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
request->time = time;
|
||||
request->position = position;
|
||||
request->next = NULL;
|
||||
|
||||
if (queue->head == NULL) {
|
||||
queue->head = request;
|
||||
} else {
|
||||
DiskRequest *current = queue->head;
|
||||
while (current->next != NULL) {
|
||||
current = current->next;
|
||||
}
|
||||
current->next = request;
|
||||
}
|
||||
queue->size++;
|
||||
}
|
||||
|
||||
DiskRequest *delete(DiskQueue *queue, DiskRequest request) {
|
||||
DiskRequest *current = queue->head;
|
||||
DiskRequest *previous = NULL;
|
||||
while (current != NULL) {
|
||||
if (current->position == request.position && current->time == request.time) {
|
||||
if (previous == NULL) {
|
||||
queue->head = current->next;
|
||||
} else {
|
||||
previous->next = current->next;
|
||||
}
|
||||
queue->size--;
|
||||
return current;
|
||||
}
|
||||
previous = current;
|
||||
current = current->next;
|
||||
}
|
||||
printf("Error: Request not found\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
DiskRequest peek(DiskQueue *queue) {
|
||||
return *queue->head;
|
||||
}
|
||||
|
||||
DiskRequest dequeue(DiskQueue *queue) {
|
||||
DiskRequest *head = queue->head;
|
||||
queue->head = head->next;
|
||||
queue->size--;
|
||||
return *head;
|
||||
}
|
||||
|
||||
void destroyDiskQueue(DiskQueue *queue) {
|
||||
DiskRequest *current = queue->head;
|
||||
DiskRequest *next = NULL;
|
||||
while (current != NULL) {
|
||||
next = current->next;
|
||||
free(current);
|
||||
current = next;
|
||||
}
|
||||
free(queue);
|
||||
}
|
||||
|
||||
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) {
|
||||
closest = current;
|
||||
} else {
|
||||
if (abs(current->position - position) < abs(closest->position - position)) {
|
||||
closest = current;
|
||||
}
|
||||
}
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
if (closest == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
return delete(queue, *closest);
|
||||
}
|
||||
|
@ -1,12 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
typedef struct DiskRequest {
|
||||
struct DiskRequest *next;
|
||||
int position;
|
||||
int time;
|
||||
} DiskRequest;
|
||||
|
||||
typedef struct DiskQueue {
|
||||
DiskRequest *requests;
|
||||
DiskRequest *head;
|
||||
int size;
|
||||
} DiskQueue;
|
||||
|
||||
@ -16,23 +17,14 @@ typedef enum DiskDirection {
|
||||
|
||||
DiskQueue *createDiskQueue();
|
||||
|
||||
DiskRequest diskRequest(int position, int time);
|
||||
void enqueue(DiskQueue *queue, int time, int position);
|
||||
|
||||
void enqueue(DiskQueue *queue, DiskRequest request);
|
||||
|
||||
DiskRequest delete(DiskQueue *queue, DiskRequest request);
|
||||
|
||||
DiskQueue *clone(DiskQueue *queue);
|
||||
DiskRequest *delete(DiskQueue *queue, DiskRequest request);
|
||||
|
||||
DiskRequest peek(DiskQueue *queue);
|
||||
|
||||
DiskRequest dequeue(DiskQueue *queue);
|
||||
|
||||
DiskQueue *queueOfLesserTime(DiskQueue *queue, double time);
|
||||
|
||||
int compareDiskRequestsByPosition(const void *a, const void *b);
|
||||
|
||||
DiskQueue *sortQueueByPosition(DiskQueue *queue);
|
||||
DiskRequest *findClosest(DiskQueue *queue, int position, double time);
|
||||
|
||||
void destroyDiskQueue(DiskQueue *queue);
|
||||
|
||||
|
@ -30,9 +30,7 @@ double timeToProcessRequest(int position, int destination) {
|
||||
// In first come first the time the request comes in is irrelevant
|
||||
void fcfs(int start) {
|
||||
int position = start;
|
||||
int size = queue->size;
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
while (queue->size > 0) {
|
||||
DiskRequest request = dequeue(queue);
|
||||
while (seekTime < request.time) { seekTime++; }
|
||||
seekTime += timeToProcessRequest(position, request.position);
|
||||
@ -44,33 +42,23 @@ void fcfs(int start) {
|
||||
void cscan(int start) {
|
||||
int position = start;
|
||||
|
||||
DiskQueue *workingQueue = clone(queue);
|
||||
|
||||
while (workingQueue->size > 0) {
|
||||
DiskQueue *candidates = queueOfLesserTime(workingQueue, seekTime);
|
||||
|
||||
if (candidates->size == 0) {
|
||||
while (seekTime < peek(workingQueue).time) { seekTime++; }
|
||||
continue;
|
||||
}
|
||||
sortQueueByPosition(candidates);
|
||||
DiskRequest request = dequeue(candidates);
|
||||
|
||||
if (request.position < position) {
|
||||
// Go to end of disk and back to 0
|
||||
while (queue->size > 0) {
|
||||
DiskRequest *request = findClosest(queue, position, seekTime);
|
||||
if (request == NULL) {
|
||||
seekTime++;
|
||||
} else {
|
||||
if (request->position < position) {
|
||||
seekTime += timeToProcessRequest(position, 9999);
|
||||
seekTime += timeToProcessRequest(9999, 0);
|
||||
movement += abs(position - 9999);
|
||||
movement += abs(9999 - 0);
|
||||
position = 0;
|
||||
}
|
||||
seekTime += timeToProcessRequest(position, request.position);
|
||||
movement += abs(position - request.position);
|
||||
position = request.position;
|
||||
delete(workingQueue, request);
|
||||
destroyDiskQueue(candidates);
|
||||
seekTime += timeToProcessRequest(position, request->position);
|
||||
movement += abs(position - request->position);
|
||||
position = request->position;
|
||||
}
|
||||
}
|
||||
destroyDiskQueue(workingQueue);
|
||||
}
|
||||
|
||||
|
||||
@ -82,7 +70,7 @@ int main(int argc, char **argv) {
|
||||
queue = createDiskQueue();
|
||||
|
||||
while (EOF != (scanf("%i %i\n", &position, &time))) {
|
||||
enqueue(queue, diskRequest(position, time));
|
||||
enqueue(queue, time, position);
|
||||
// printf("Delete me: position %i, Delete me: time %i\n", position, time);
|
||||
}
|
||||
if (algorithm == 'F') {
|
||||
|
Loading…
Reference in New Issue
Block a user