1
1
mirror of https://github.com/theoludwig/libcproject.git synced 2024-12-11 21:13:00 +01:00
libcproject/lib/queue.c

70 lines
1.6 KiB
C
Raw Normal View History

2023-01-05 19:28:05 +01:00
#include "queue.h"
struct queue *queue_initialization() {
struct queue *queue = malloc(sizeof(struct queue));
2023-01-05 19:28:05 +01:00
if (queue == NULL) {
perror("Error (queue_initialization)");
2023-01-05 19:28:05 +01:00
exit(EXIT_FAILURE);
}
queue->first = NULL;
queue->length = 0;
return queue;
}
void queue_push(struct queue *queue, void *data) {
if (queue == NULL) {
errno = EINVAL;
perror("Error (queue_push)");
exit(EXIT_FAILURE);
}
struct queue_node *node_new = malloc(sizeof(struct queue_node));
if (node_new == NULL) {
perror("Error (queue_push)");
2023-01-05 19:28:05 +01:00
exit(EXIT_FAILURE);
}
node_new->data = data;
node_new->next = NULL;
if (queue->first != NULL) {
struct queue_node *node_current = queue->first;
while (node_current->next != NULL) {
node_current = node_current->next;
}
node_current->next = node_new;
} else {
queue->first = node_new;
}
queue->length = queue->length + 1;
}
void *queue_pop(struct queue *queue) {
if (queue == NULL) {
errno = EINVAL;
perror("Error (queue_pop)");
2023-01-05 19:28:05 +01:00
exit(EXIT_FAILURE);
}
struct queue_node *node = queue->first;
void *data = NULL;
if (node != NULL) {
queue->first = node->next;
data = node->data;
free(node);
}
queue->length = queue->length - 1;
return data;
}
2023-08-03 22:57:32 +02:00
void queue_free(struct queue *queue) {
if (queue == NULL) {
errno = EINVAL;
perror("Error (queue_free)");
2023-08-03 22:57:32 +02:00
exit(EXIT_FAILURE);
}
struct queue_node *node = queue->first;
while (node != NULL) {
struct queue_node *node_next = node->next;
free(node);
node = node_next;
}
free(queue);
}