#include "queue.h"
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+void QueueInit(Queue *q, size_t memSize)
+{
+ assert(q != NULL);
+ assert(memSize > 0);
+
+ q->memSize = memSize;
+ q->tail = NULL;
+ q->head = NULL;
+}
+
+void QueuePush(Queue *q, const void *data)
+{
+ assert(q != NULL);
+ QueueNode *n = malloc(sizeof(QueueNode));
+
+ assert(n != NULL);
+ n->data = malloc(q->memSize);
+ assert(n->data != NULL);
+ n->next = NULL;
+
+ memcpy(n->data, data, q->memSize);
+
+ if(q->head == NULL) {
+ q->head = q->tail = n;
+ } else {
+ q->tail->next = n;
+ q->tail = n;
+ }
+}
+
+void QueuePop(Queue *q, void *data)
+{
+ assert(q != NULL);
+ assert(q->head != NULL);
+
+ QueueNode *tmp = q->head;
+
+ if(data != NULL) {
+ memcpy(data, q->head->data, q->memSize);
+ }
+ q->head = q->head->next;
+
+ if(q->head == NULL) {
+ q->tail = NULL;
+ }
+
+ free(tmp->data);
+ free(tmp);
+}
+
+void QueuePeek(Queue *q, void *data)
+{
+ assert(q != NULL);
+ assert(q->head != NULL);
+ assert(q->head->data != NULL);
+ assert(data != NULL);
+
+ memcpy(data, q->head->data, q->memSize);
+}
+
+void QueueClear(Queue *q)
+{
+ assert(q != NULL);
+ if(q->head == NULL) { return; }
+
+ int count = 0;
+ QueueNode *tmp = NULL;
+ do {
+ tmp = q->head;
+ count++;
+ q->head = tmp->next;
+ free(tmp->data);
+ free(tmp);
+ }
+ while(tmp != q->tail);
+
+ q->head = q->tail = NULL;
+}
+
+size_t QueueGetSize(Queue *q)
+{
+ size_t size = 0;
+ assert(q != NULL);
+ if(q->head == NULL) { return 0; }
+
+ QueueNode *head = q->head;
+ do {
+ size++;
+ head = head->next;
+ } while(head != q->tail);
+
+
+ return size;
+}
#pragma once
+#include <stddef.h>
+
+typedef struct QueueNode {
+ void *data;
+ struct QueueNode *next;
+} QueueNode;
+
+typedef struct Queue {
+ size_t memSize;
+ QueueNode *tail;
+ QueueNode *head;
+} Queue;
+
+void QueueInit(Queue *q, size_t memSize);
+void QueuePush(Queue *q, const void *data);
+void QueuePop(Queue *q, void *data);
+void QueuePeek(Queue *q, void *data);
+void QueueClear(Queue *q);
+size_t QueueGetSize(Queue *q);