shithub: rott

Download patch

ref: df7921e87ea437ed41cee021549256a4a8cd56fb
parent: 08d9d6218e54dfb8d370c9c8b2364e734b9644db
author: levesqu8 <levesqu8@msu.edu>
date: Mon Nov 20 12:07:26 EST 2017

forgot queue files. Oops!

--- /dev/null
+++ b/rott/queue.c
@@ -1,0 +1,108 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "Queue.h"
+
+//Fetched from https://codereview.stackexchange.com/questions/141238/implementing-a-generic-queue-in-c
+
+
+void queueInit(Queue *q, size_t memSize)
+{
+   q->sizeOfQueue = 0;
+   q->memSize = memSize;
+   q->head = q->tail = NULL;
+}
+
+int enqueue(Queue *q, const void *data)
+{
+    node *newNode = (node *)malloc(sizeof(node));
+
+    if(newNode == NULL)
+    {
+        return -1;
+    }
+
+    newNode->data = malloc(q->memSize);
+
+    if(newNode->data == NULL)
+    {
+        free(newNode);
+        return -1;
+    }
+
+    newNode->next = NULL;
+
+    memcpy(newNode->data, data, q->memSize);
+
+    if(q->sizeOfQueue == 0)
+    {
+        q->head = q->tail = newNode;
+    }
+    else
+    {
+        q->tail->next = newNode;
+        q->tail = newNode;
+    }
+
+    q->sizeOfQueue++;
+    return 0;
+}
+
+void dequeue(Queue *q, void *data)
+{
+    if(q->sizeOfQueue > 0)
+    {
+        node *temp = q->head;
+        memcpy(data, temp->data, q->memSize);
+
+        if(q->sizeOfQueue > 1)
+        {
+            q->head = q->head->next;
+        }
+        else
+        {
+            q->head = NULL;
+            q->tail = NULL;
+        }
+
+        q->sizeOfQueue--;
+        free(temp->data);
+        free(temp);
+    }
+}
+
+void queuePeek(Queue *q, void *data)
+{
+    if(q->sizeOfQueue > 0)
+    {
+       node *temp = q->head;
+       memcpy(data, temp->data, q->memSize);
+    }
+}
+
+void clearQueue(Queue *q)
+{
+  node *temp;
+
+  while(q->sizeOfQueue > 0)
+  {
+      temp = q->head;
+      q->head = temp->next;
+      free(temp->data);
+      free(temp);
+      q->sizeOfQueue--;
+  }
+
+  q->head = q->tail = NULL;
+}
+
+int getQueueSize(Queue *q)
+{
+    return q->sizeOfQueue;
+}
\ No newline at end of file
--- /dev/null
+++ b/rott/queue.h
@@ -1,0 +1,28 @@
+#ifndef QUEUE_H_INCLUDED
+#define QUEUE_H_INCLUDED
+
+//Fetched from https://codereview.stackexchange.com/questions/141238/implementing-a-generic-queue-in-c
+
+
+typedef struct Node
+{
+  void *data;
+  struct Node *next;
+}node;
+
+typedef struct QueueList
+{
+    int sizeOfQueue;
+    size_t memSize;
+    node *head;
+    node *tail;
+}Queue;
+
+void queueInit(Queue *q, size_t memSize);
+int enqueue(Queue *, const void *);
+void dequeue(Queue *, void *);
+void queuePeek(Queue *, void *);
+void clearQueue(Queue *);
+int getQueueSize(Queue *);
+
+#endif /* QUEUE_H_INCLUDED */
\ No newline at end of file