From: Matthew Owens <matthew@owens.tech>
Date: Sun, 26 Dec 2021 17:14:02 +0000 (+0000)
Subject: bugfixes
X-Git-Url: https://git.owens.tech///git?a=commitdiff_plain;p=AbyssalThistle%2Fpriority-queue.git

bugfixes
---

diff --git a/src/priority_queue.c b/src/priority_queue.c
index 1a5a65b..5b527f0 100644
--- a/src/priority_queue.c
+++ b/src/priority_queue.c
@@ -2,6 +2,7 @@
 #include <assert.h>
 #include <stdlib.h>
 #include <string.h>
+#include <stdio.h>
 
 void PrioQueueInit(PrioQueue *q, size_t memSize)
 {
@@ -26,7 +27,7 @@ static void prioQueueInsert(PrioQueue *q, PrioQueueNode *n)
 		// do we need to insert before or after?
 		if(n->priority > current->priority) {
 			n->next = current;
-			q->head = n->next;
+			q->head = n;
 			current->next = NULL;
 			return;
 		} else {
@@ -40,12 +41,18 @@ static void prioQueueInsert(PrioQueue *q, PrioQueueNode *n)
 			// ensuring we have prev populated
 			if(prev == NULL) {
 				prev = current;
+				current = current->next;
 				continue;
 			}
-			if(prev->priority > n->priority > current->priority) {
+			if(prev->priority > n->priority && n->priority > current->priority) {
 				n->next = current;
 				prev->next = n;
 				return;
+			} else if(n->priority > prev->priority && prev->priority > current->priority) {
+				// must've been a head insert
+				n->next = prev;
+				q->head = n;
+				return;
 			}
 			prev = current;
 			current = current->next;
@@ -141,3 +148,15 @@ size_t PrioQueueGetSize(PrioQueue *q)
 
 	return size;
 }
+
+void PrioQueuePrint(PrioQueue *q)
+{
+	PrioQueueNode *n = q->head;
+	int i = 0;
+
+	do {
+		printf("%d - data: %d, priority: %d\n", i, *( int*)n->data, n->priority);
+		++i;
+		n = n->next;
+	} while(n != NULL);
+}
diff --git a/src/priority_queue.h b/src/priority_queue.h
index fce0557..172420b 100644
--- a/src/priority_queue.h
+++ b/src/priority_queue.h
@@ -19,3 +19,4 @@ void PrioQueuePop(PrioQueue *q, void *data);
 void PrioQueuePeek(PrioQueue *q, void *data);
 void PrioQueueClear(PrioQueue *q);
 size_t PrioQueueGetSize(PrioQueue *q);
+void PrioQueuePrint(PrioQueue *q);