aboutsummaryrefslogtreecommitdiff
path: root/pa6_threaded_resolver/array.c
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2024-04-02 23:09:33 -0600
committerJack Sangdahl <[email protected]>2024-04-02 23:09:33 -0600
commit7ddf686201d052e7c55d622dddd62b7e2ede880d (patch)
tree1ce98638df78223b5a42ef8ba7aa7b1f5d49941a /pa6_threaded_resolver/array.c
parentf3172b837cc728dec1daa74ce98a72548f204b21 (diff)
downloadcsci3753-7ddf686201d052e7c55d622dddd62b7e2ede880d.tar.gz
csci3753-7ddf686201d052e7c55d622dddd62b7e2ede880d.zip
pa6
Diffstat (limited to 'pa6_threaded_resolver/array.c')
-rw-r--r--pa6_threaded_resolver/array.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/pa6_threaded_resolver/array.c b/pa6_threaded_resolver/array.c
new file mode 100644
index 0000000..e43dbdf
--- /dev/null
+++ b/pa6_threaded_resolver/array.c
@@ -0,0 +1,48 @@
+// Jack Sangdahl
+// CSCI3753 SP24
+// PA6 Threaded Resolver
+#include "array.h"
+
+int queue_init(queue *q, int queue_size) {
+ q->data = malloc(queue_size * sizeof(node));
+ q->maxsize = queue_size;
+ q->front = 0;
+ q->back = -1;
+ q->size = 0;
+ return EXIT_SUCCESS;
+}
+
+int queue_size(queue *q) { return q->size; }
+
+bool queue_full(queue *q) { return (q->size == q->maxsize); }
+
+int queue_push(queue *q, void *element) {
+ if (queue_full(q)) {
+ fprintf(stderr, "push error: queue full\n");
+ return EXIT_FAILURE;
+ }
+ q->back = (q->back + 1) % q->maxsize;
+ q->data[q->back].value = element;
+ q->size++;
+ return EXIT_SUCCESS;
+}
+
+void *queue_pop(queue *q) {
+ if (!queue_size(q)) {
+ fprintf(stderr, "pop error: queue empty\n");
+ return NULL;
+ }
+ void *value = q->data[q->front].value;
+ q->data[q->front].value = NULL;
+ q->front = (q->front + 1) % q->maxsize;
+ q->size--;
+ return value;
+}
+
+void queue_free(queue *q) {
+ while (q->size > 0) {
+ void *val = queue_pop(q);
+ free(val);
+ }
+ free(q->data);
+}