]> wirehaze git hosting - ppos.git/commitdiff

wirehaze git hosting

queue.c implementation
authorphfr24 <phfr24@inf.ufpr.br>
Sun, 1 Mar 2026 00:25:47 +0000 (21:25 -0300)
committerphfr24 <phfr24@inf.ufpr.br>
Sun, 1 Mar 2026 00:25:47 +0000 (21:25 -0300)
queue/queue.c

index 4afa4a50164859b822cc86b060d4eada937977c0..e54cb5e4730ae7da41d846e2f609ffef20a5577e 100644 (file)
@@ -4,7 +4,11 @@
 
 // Implementação do TAD fila genérica
 
+/* PEDRO HENRIQUE FRIEDRICH RAMOS : GRR20243133 */
+
 #include "queue.h"
+#include <stdio.h>
+#include <stdlib.h>
 
 struct node_t
 {
@@ -19,3 +23,246 @@ struct queue_t
   struct node_t *iterator;
   int size;
 };
+
+static inline struct node_t *
+node_create (void)
+{
+  return calloc (1, sizeof (struct node_t));
+}
+
+static inline void
+node_destroy (struct node_t *node)
+{
+  free (node);
+}
+
+struct queue_t *
+queue_create (void)
+{
+  return calloc (1, sizeof (struct queue_t));
+}
+
+int
+queue_destroy (struct queue_t *queue)
+{
+  struct node_t *n, *next;
+
+  if (!queue)
+    {
+      return ERROR;
+    }
+
+  if (!(n = queue->head))
+    {
+      free (queue);
+      return NOERROR;
+    }
+
+  do
+    {
+      next = n->next;
+      node_destroy (n);
+    }
+  while ((n = next));
+
+  free (queue);
+  return NOERROR;
+}
+
+int
+queue_add (struct queue_t *queue, void *item)
+{
+  struct node_t *n;
+
+  if (!queue || !item)
+    {
+      return ERROR;
+    }
+
+  if (!(n = node_create ()))
+    {
+      return ERROR;
+    }
+
+  n->item = item;
+  ++queue->size;
+
+  if (!queue->head)
+    {
+      queue->head = queue->tail = queue->iterator = n;
+      return NOERROR;
+    }
+
+  queue->tail = queue->tail->next = n;
+  return NOERROR;
+}
+
+int
+queue_del (struct queue_t *queue, void *item)
+{
+  struct node_t *n, *prev;
+
+  if (!queue || !item)
+    {
+      return ERROR;
+    }
+
+  if (!(n = queue->head))
+    {
+      return ERROR;
+    }
+
+  prev = NULL;
+
+  while (n && n->item != item)
+    {
+      prev = n;
+      n = n->next;
+    }
+
+  if (!n) /* not found */
+    {
+      return ERROR;
+    }
+
+  if (prev)
+    {
+      prev->next = n->next;
+    }
+
+  if (queue->head == n)
+    {
+      queue->head = n->next;
+    }
+
+  if (queue->tail == n)
+    {
+      queue->tail = prev;
+    }
+
+  if (queue->iterator == n)
+    {
+      queue->iterator = n->next;
+    }
+
+  --queue->size;
+  node_destroy (n);
+  return NOERROR;
+}
+
+bool
+queue_has (struct queue_t *queue, void *item)
+{
+  struct node_t *n;
+
+  if (!queue || !item)
+    {
+      return false;
+    }
+
+  if (!(n = queue->head))
+    {
+      return false;
+    }
+
+  while (n->item != item && (n = n->next))
+    ;
+
+  if (!n)
+    {
+      return false;
+    }
+
+  return true;
+}
+
+int
+queue_size (struct queue_t *queue)
+{
+  if (!queue)
+    {
+      return ERROR;
+    }
+
+  return queue->size;
+}
+
+void *
+queue_head (struct queue_t *queue)
+{
+  if (!queue || !queue->head)
+    {
+      return NULL;
+    }
+
+  queue->iterator = queue->head;
+
+  return queue->head->item;
+}
+
+void *
+queue_next (struct queue_t *queue)
+{
+  if (!queue || !queue->iterator)
+    {
+      return NULL;
+    }
+
+  queue->iterator = queue->iterator->next;
+
+  if (!queue->iterator)
+    {
+      return NULL;
+    }
+
+  return queue->iterator->item;
+}
+
+void *
+queue_item (struct queue_t *queue)
+{
+  if (!queue || !queue->iterator)
+    {
+      return NULL;
+    }
+
+  return queue->iterator->item;
+}
+
+void
+queue_print (char *name, struct queue_t *queue, void (func) (void *))
+{
+  struct node_t *n;
+
+  if (!name)
+    {
+      return;
+    }
+
+  printf ("%s: ", name);
+
+  if (!queue)
+    {
+      puts ("undef");
+      return;
+    }
+
+  fputs ("[ ", stdout);
+
+  for (n = queue->head; n; n = n->next)
+    {
+      if (func)
+        {
+          func (n->item);
+        }
+
+      else
+        {
+          fputs ("undef", stdout);
+        }
+
+      putchar (' ');
+    }
+
+  printf ("] (%d items)\n", queue->size);
+}