Move to linked list implementation

This commit is contained in:
Isaac Shoebottom 2023-11-28 21:02:49 -04:00
parent fb6ec1dc98
commit 6189cf6a3b
3 changed files with 108 additions and 132 deletions

View File

@ -2,91 +2,6 @@
#include <stdio.h> #include <stdio.h>
#include "disk.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 *createDiskQueue() {
DiskQueue *queue = malloc(sizeof(DiskQueue)); DiskQueue *queue = malloc(sizeof(DiskQueue));
@ -94,11 +9,92 @@ DiskQueue *createDiskQueue() {
printf("Error allocating memory for queue\n"); printf("Error allocating memory for queue\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
queue->requests = malloc(sizeof(DiskRequest)); queue->head = NULL;
if (queue->requests == NULL) {
printf("Error allocating memory for queue requests\n");
exit(EXIT_FAILURE);
}
queue->size = 0; queue->size = 0;
return queue; 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);
}

View File

@ -1,12 +1,13 @@
#pragma once #pragma once
typedef struct DiskRequest { typedef struct DiskRequest {
struct DiskRequest *next;
int position; int position;
int time; int time;
} DiskRequest; } DiskRequest;
typedef struct DiskQueue { typedef struct DiskQueue {
DiskRequest *requests; DiskRequest *head;
int size; int size;
} DiskQueue; } DiskQueue;
@ -16,23 +17,14 @@ typedef enum DiskDirection {
DiskQueue *createDiskQueue(); 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);
DiskRequest delete(DiskQueue *queue, DiskRequest request);
DiskQueue *clone(DiskQueue *queue);
DiskRequest peek(DiskQueue *queue); DiskRequest peek(DiskQueue *queue);
DiskRequest dequeue(DiskQueue *queue); DiskRequest dequeue(DiskQueue *queue);
DiskQueue *queueOfLesserTime(DiskQueue *queue, double time); DiskRequest *findClosest(DiskQueue *queue, int position, double time);
int compareDiskRequestsByPosition(const void *a, const void *b);
DiskQueue *sortQueueByPosition(DiskQueue *queue);
void destroyDiskQueue(DiskQueue *queue); void destroyDiskQueue(DiskQueue *queue);

View File

@ -30,9 +30,7 @@ double timeToProcessRequest(int position, int destination) {
// In first come first the time the request comes in is irrelevant // In first come first the time the request comes in is irrelevant
void fcfs(int start) { void fcfs(int start) {
int position = start; int position = start;
int size = queue->size; while (queue->size > 0) {
for (int i = 0; i < size; i++) {
DiskRequest request = dequeue(queue); DiskRequest request = dequeue(queue);
while (seekTime < request.time) { seekTime++; } while (seekTime < request.time) { seekTime++; }
seekTime += timeToProcessRequest(position, request.position); seekTime += timeToProcessRequest(position, request.position);
@ -44,33 +42,23 @@ void fcfs(int start) {
void cscan(int start) { void cscan(int start) {
int position = start; int position = start;
DiskQueue *workingQueue = clone(queue); while (queue->size > 0) {
DiskRequest *request = findClosest(queue, position, seekTime);
while (workingQueue->size > 0) { if (request == NULL) {
DiskQueue *candidates = queueOfLesserTime(workingQueue, seekTime); seekTime++;
} else {
if (candidates->size == 0) { if (request->position < position) {
while (seekTime < peek(workingQueue).time) { seekTime++; } seekTime += timeToProcessRequest(position, 9999);
continue; 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;
} }
sortQueueByPosition(candidates);
DiskRequest request = dequeue(candidates);
if (request.position < position) {
// Go to end of disk and back to 0
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);
} }
destroyDiskQueue(workingQueue);
} }
@ -82,7 +70,7 @@ int main(int argc, char **argv) {
queue = createDiskQueue(); queue = createDiskQueue();
while (EOF != (scanf("%i %i\n", &position, &time))) { 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); // printf("Delete me: position %i, Delete me: time %i\n", position, time);
} }
if (algorithm == 'F') { if (algorithm == 'F') {