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