mirror of
https://github.com/theoludwig/libcproject.git
synced 2024-11-08 22:31:31 +01:00
49 lines
1.1 KiB
C
49 lines
1.1 KiB
C
|
#include "queue.h"
|
||
|
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
|
||
|
struct queue *queue_initialization() {
|
||
|
struct queue *queue = malloc(sizeof(*queue));
|
||
|
if (queue == NULL) {
|
||
|
exit(EXIT_FAILURE);
|
||
|
}
|
||
|
queue->first = NULL;
|
||
|
queue->length = 0;
|
||
|
return queue;
|
||
|
}
|
||
|
|
||
|
void queue_push(struct queue *queue, void *data) {
|
||
|
struct queue_node *node_new = malloc(sizeof(*node_new));
|
||
|
if (queue == NULL || node_new == NULL) {
|
||
|
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) {
|
||
|
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;
|
||
|
}
|