aboutsummaryrefslogtreecommitdiff
path: root/src/lib/queue.c
diff options
context:
space:
mode:
authorMarvin Borner2023-05-30 23:05:30 +0200
committerMarvin Borner2023-05-30 23:33:39 +0200
commitcbd21e1da0d763225e7ea3594d4e6d8e96863790 (patch)
treedae4242584e178b59337ca247aa532805af0d8d0 /src/lib/queue.c
parente78acdabd1436083c503a5f1860ecdf14f3ee1bd (diff)
Added hash-based approach
Diffstat (limited to 'src/lib/queue.c')
-rw-r--r--src/lib/queue.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/lib/queue.c b/src/lib/queue.c
new file mode 100644
index 0000000..f039257
--- /dev/null
+++ b/src/lib/queue.c
@@ -0,0 +1,61 @@
+// Copyright (c) 2023, Marvin Borner <dev@marvinborner.de>
+// SPDX-License-Identifier: MIT
+
+#include <stdlib.h>
+
+#include <lib/queue.h>
+#include <log.h>
+
+struct queue *queue_new(void)
+{
+ struct queue *queue = malloc(sizeof(*queue));
+ if (!queue)
+ fatal("out of memory!\n");
+ queue->head = 0;
+ queue->tail = 0;
+ return queue;
+}
+
+void queue_free(struct queue *queue)
+{
+ while (queue->head) {
+ struct queue_node *node = queue->head;
+ queue->head = node->next;
+ free(node);
+ }
+ free(queue);
+}
+
+void queue_push(struct queue *queue, void *data)
+{
+ struct queue_node *node = malloc(sizeof(*node));
+ if (!node)
+ fatal("out of memory!\n");
+ node->data = data;
+ node->next = 0;
+ if (queue->tail) {
+ queue->tail->next = node;
+ queue->tail = node;
+ } else {
+ queue->head = node;
+ queue->tail = node;
+ }
+}
+
+void *queue_pop(struct queue *queue)
+{
+ if (!queue->head)
+ return 0;
+ struct queue_node *node = queue->head;
+ queue->head = node->next;
+ if (!queue->head)
+ queue->tail = 0;
+ void *data = node->data;
+ free(node);
+ return data;
+}
+
+int queue_empty(struct queue *queue)
+{
+ return !queue->head;
+}