Files
stergios/src/queue.c
T

66 lines
973 B
C

#include "queue.h"
#include <stdio.h>
#include <stdlib.h>
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 = 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 (size == capacity) return;
queue[tail] = n;
tail++;
if (tail == capacity) tail = 0;
size++;
}
Node* queue_pop() {
if (size == 0) return NULL;
Node* r = queue[head];
head++;
if (head == capacity) head = 0;
size--;
return r;
}
int32_t queue_size() {
return size;
}
void queue_clear() {
head = 0;
tail = 0;
size = 0;
}
void queue_destroy() {
if (queue) {
free(queue);
queue = NULL;
}
head = tail = size = capacity = 0;
}