Complete rework of the queueing system. Adding some more helper functions in maths.c. FInally, adding valgrind (a debugger) in the nix flaes file.
Stergios Upload Compile Checker / Explore-Gitea-Actions (push) Successful in 16s
Stergios Upload Compile Checker / Explore-Gitea-Actions (push) Successful in 16s
This commit is contained in:
9 files changed
+79
-62
No files matched your search
+41
-38
@@ -1,63 +1,66 @@
|
||||
#include "queue.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static Node* queue_front = NULL;
|
||||
static Node* queue_back = NULL;
|
||||
static int32_t current_size = 0;
|
||||
static int32_t capacity = 0;
|
||||
static Node** queue;
|
||||
static int32_t head;
|
||||
static int32_t tail;
|
||||
static int32_t size;
|
||||
static int32_t capacity;
|
||||
|
||||
void queue_init(int32_t max_elements) {
|
||||
capacity = max_elements;
|
||||
queue_front = NULL;
|
||||
queue_back = NULL;
|
||||
current_size = 0;
|
||||
queue = malloc(capacity * sizeof(*queue));
|
||||
|
||||
if (!queue) {
|
||||
fprintf(stderr,"Queue allocation failed\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
head = 0;
|
||||
tail = 0;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
void queue_push(Node* n) {
|
||||
if (current_size >= capacity) return; /* Pool safety */
|
||||
if (size == capacity) return;
|
||||
|
||||
/* Exact logic from your 'enqueue' function */
|
||||
n->next = NULL;
|
||||
if (!queue_back) {
|
||||
queue_front = queue_back = n;
|
||||
} else {
|
||||
queue_back->next = n;
|
||||
queue_back = n;
|
||||
}
|
||||
current_size++;
|
||||
queue[tail] = n;
|
||||
tail++;
|
||||
|
||||
if (tail == capacity) tail = 0;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
Node* queue_pop() {
|
||||
/* Exact logic from your 'dequeue' function */
|
||||
if (!queue_front) return NULL;
|
||||
|
||||
Node* r = queue_front;
|
||||
queue_front = queue_front->next;
|
||||
|
||||
if (!queue_front) {
|
||||
queue_back = NULL;
|
||||
}
|
||||
|
||||
r->next = NULL; /* Clean up the link */
|
||||
current_size--;
|
||||
|
||||
if (size == 0) return NULL;
|
||||
|
||||
Node* r = queue[head];
|
||||
head++;
|
||||
|
||||
if (head == capacity) head = 0;
|
||||
|
||||
size--;
|
||||
return r;
|
||||
}
|
||||
|
||||
int32_t queue_size() {
|
||||
return current_size;
|
||||
return size;
|
||||
}
|
||||
|
||||
void queue_clear() {
|
||||
queue_front = NULL;
|
||||
queue_back = NULL;
|
||||
current_size = 0;
|
||||
head = 0;
|
||||
tail = 0;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
void queue_destroy() {
|
||||
queue_front = NULL;
|
||||
queue_back = NULL;
|
||||
current_size = 0;
|
||||
capacity = 0;
|
||||
if (queue) {
|
||||
free(queue);
|
||||
queue = NULL;
|
||||
}
|
||||
|
||||
head = tail = size = capacity = 0;
|
||||
}
|
||||
Reference in new issue
Block a user