ppos

Usermode educational operating system.
Log | Files | Refs

queue.c (3445B)


      1 // PingPongOS - PingPong Operating System
      2 // © Prof. Carlos A. Maziero, DINF UFPR
      3 // Versão 2.1 -- 06/2026
      4 
      5 // Este arquivo PODE/DEVE ser alterado.
      6 
      7 // Implementação do TAD fila genérica
      8 
      9 #include "queue.h"
     10 
     11 #include <stdio.h>
     12 #include <stdlib.h>
     13 
     14 typedef struct node_t {
     15     struct node_t* next;
     16     void*          item;
     17 } node_t;
     18 
     19 struct queue_t {
     20     node_t* head;
     21     node_t* tail;
     22     node_t* it;
     23 };
     24 
     25 struct queue_t* queue_create()
     26 {
     27     return (struct queue_t*) calloc(1, sizeof(struct queue_t));
     28 }
     29 
     30 int queue_destroy(struct queue_t* queue)
     31 {
     32     if (queue == NULL)
     33         return ERROR;
     34 
     35     node_t* node = queue->head;
     36     while (node != NULL) {
     37         node_t* n = node;
     38         node      = node->next;
     39         free(n);
     40     }
     41 
     42     free(queue);
     43 
     44     return NOERROR;
     45 }
     46 
     47 int queue_add(struct queue_t* queue, void* item)
     48 {
     49     if (queue == NULL)
     50         return ERROR;
     51 
     52     node_t* new = malloc(sizeof(*new));
     53     new->item   = item;
     54     new->next   = NULL;
     55 
     56     if (queue->tail == NULL) {
     57         queue->tail = new;
     58         queue->head = new;
     59         queue->it   = new;
     60     } else {
     61         queue->tail->next = new;
     62         queue->tail       = new;
     63     }
     64 
     65     return NOERROR;
     66 }
     67 
     68 int queue_del(struct queue_t* queue, void* item)
     69 {
     70     if (queue == NULL)
     71         return ERROR;
     72     // Works because of the layout of the structs.
     73     struct node_t* parent = (struct node_t*) queue;
     74     struct node_t* node   = queue->head;
     75     while (node != NULL) {
     76         if (node->item == item) {
     77             parent->next = node->next;
     78             if (queue->it == node)
     79                 queue->it = node->next;
     80             if (queue->tail == node) {
     81                 if (parent == (struct node_t*) queue) {
     82                     queue->head = NULL;
     83                     queue->tail = NULL;
     84                 } else {
     85                     queue->tail = parent;
     86                 }
     87             }
     88             free(node);
     89             return NOERROR;
     90         }
     91         parent = node;
     92         node   = node->next;
     93     }
     94 
     95     return ERROR;
     96 }
     97 
     98 bool queue_has(struct queue_t* queue, void* item)
     99 {
    100     for (node_t* node = queue->head; node != NULL; node = node->next) {
    101         if (node->item == item)
    102             return true;
    103     }
    104     return false;
    105 }
    106 
    107 int queue_size(struct queue_t* queue)
    108 {
    109     if (queue == NULL)
    110         return ERROR;
    111 
    112     int n = 0;
    113     for (node_t* node = queue->head; node != NULL; node = node->next)
    114         n += 1;
    115     return n;
    116 }
    117 
    118 void* queue_head(struct queue_t* queue)
    119 {
    120     if (queue == NULL)
    121         return NULL;
    122     queue->it = queue->head;
    123     if (queue->head == NULL)
    124         return NULL;
    125     return queue->head->item;
    126 }
    127 
    128 void* queue_next(struct queue_t* queue)
    129 {
    130     if (queue == NULL || queue->it == NULL)
    131         return NULL;
    132 
    133     queue->it = queue->it->next;
    134     if (queue->it == NULL)
    135         return NULL;
    136 
    137     return queue->it->item;
    138 }
    139 
    140 void* queue_item(struct queue_t* queue)
    141 {
    142     if (queue == NULL || queue->it == NULL)
    143         return NULL;
    144     return queue->it->item;
    145 }
    146 
    147 void queue_print(char* name, struct queue_t* queue, void(func)(void*))
    148 {
    149     printf("%s: ", name);
    150     if (queue == NULL) {
    151         printf("undef\n");
    152         return;
    153     }
    154     printf("[ ");
    155 
    156     int i = 0;
    157     for (node_t* node = queue->head; node != NULL; node = node->next) {
    158         i += 1;
    159         if (func != NULL)
    160             func(node->item);
    161         else
    162             printf("undef");
    163         printf(" ");
    164     }
    165 
    166     printf("] (%d items)\n", i);
    167 }