CS3413/Assignment4/lib/queue.c

104 lines
2.9 KiB
C
Raw Normal View History

2023-10-24 13:50:05 -03:00
#include <stdio.h>
#include <stddef.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include "process.h"
#include "queue.h"
/*
* Queue implementation
* 5 4 3 2 1
* START END
*
* If you were to visualize the queue as a line, the end is the first person in line and the start is the last person in line
* So when you enqueue, you are adding to the start of the line
* And when you dequeue, you are exiting from the end of the line
*/
2023-10-26 17:15:05 -03:00
int contains(Queue *queue, char *username) {
Process *current = queue->end;
2023-10-24 13:50:05 -03:00
while (current != NULL) {
2023-10-26 17:15:05 -03:00
if (strcmp(current->username, username) == 0) {
2023-10-24 13:50:05 -03:00
return true;
}
2023-10-26 17:15:05 -03:00
current = current->prev_elem;
2023-10-24 13:50:05 -03:00
}
return false;
}
2023-10-26 17:15:05 -03:00
Process *search(Queue *queue, char *username) {
Process *current = queue->end;
2023-10-24 13:50:05 -03:00
while (current != NULL) {
2023-10-26 17:15:05 -03:00
if (strcmp(current->username, username) == 0) {
2023-10-24 13:50:05 -03:00
return current;
}
2023-10-26 17:15:05 -03:00
current = current->prev_elem;
2023-10-24 13:50:05 -03:00
}
return NULL;
}
void enqueue(Queue *queue, Process *process) {
if (queue->end == NULL) { // If the queue is empty, set the start and end to the new process
queue->end = process;
queue->start = queue->end;
} else {
process->next_elem = queue->start; // Set the next element of the new process to the start of the queue
queue->start->prev_elem = process; // Set the previous element of the start of the queue to the new process
queue->start = process; // Set the start of the queue to the new process
}
queue->size++;
}
// WARNING: Returns a pointer to a process that is not in the queue, it is your responsibility to free it
Process *dequeue(Queue *queue) {
if (queue->end == NULL) { // If the queue is empty, return NULL
return NULL;
}
2023-10-26 15:58:58 -03:00
Process *temp = queue->end; // Store the end of the queue for returning later
2023-10-24 13:50:05 -03:00
2023-10-26 15:58:58 -03:00
if (queue->size == 1) { // If the queue has one element, set the start and end to NULL
queue->start = NULL;
queue->end = NULL;
queue->size--;
return temp;
}
2023-10-24 13:50:05 -03:00
queue->end = queue->end->prev_elem; // Set the end to the previous element
2023-10-26 15:58:58 -03:00
queue->end->next_elem = NULL; // Set the next element of the new end to NULL
2023-10-24 13:50:05 -03:00
temp->prev_elem = NULL; // The dequeued element should not point to anything
queue->size--;
return temp;
}
void printList(Queue *queue) {
2023-10-26 17:15:05 -03:00
Process *current = queue->end;
2023-10-24 13:50:05 -03:00
while (current != NULL) {
2023-10-26 17:15:05 -03:00
printf("%s\t%d\n", current->username, current->finish_time);
current = current->prev_elem;
2023-10-24 13:50:05 -03:00
}
}
int stop(Queue *queue) {
Process *current = queue->end;
while (current != NULL) {
Process *next = current->prev_elem;
destroyProcess(current);
current = next;
}
free(queue);
return true;
}
Queue *createQueue() {
Queue *queue = calloc(1, sizeof(Queue));
if (queue == NULL) {
printf("Error allocating memory for queue\n");
exit(EXIT_FAILURE);
}
queue->start = NULL;
queue->end = NULL;
queue->size = 0;
return queue;
}