]>
wirehaze git hosting - ppos.git/blob - queue/queue.c
e54cb5e4730ae7da41d846e2f609ffef20a5577e
1 // PingPongOS - PingPong Operating System
2 // Prof. Carlos A. Maziero, DINF UFPR
3 // Versão 2.0 -- Junho de 2025
5 // Implementação do TAD fila genérica
7 /* PEDRO HENRIQUE FRIEDRICH RAMOS : GRR20243133 */
23 struct node_t
*iterator
;
27 static inline struct node_t
*
30 return calloc (1, sizeof (struct node_t
));
34 node_destroy (struct node_t
*node
)
42 return calloc (1, sizeof (struct queue_t
));
46 queue_destroy (struct queue_t
*queue
)
48 struct node_t
*n
, *next
;
55 if (!(n
= queue
->head
))
73 queue_add (struct queue_t
*queue
, void *item
)
82 if (!(n
= node_create ()))
92 queue
->head
= queue
->tail
= queue
->iterator
= n
;
96 queue
->tail
= queue
->tail
->next
= n
;
101 queue_del (struct queue_t
*queue
, void *item
)
103 struct node_t
*n
, *prev
;
110 if (!(n
= queue
->head
))
117 while (n
&& n
->item
!= item
)
123 if (!n
) /* not found */
130 prev
->next
= n
->next
;
133 if (queue
->head
== n
)
135 queue
->head
= n
->next
;
138 if (queue
->tail
== n
)
143 if (queue
->iterator
== n
)
145 queue
->iterator
= n
->next
;
154 queue_has (struct queue_t
*queue
, void *item
)
163 if (!(n
= queue
->head
))
168 while (n
->item
!= item
&& (n
= n
->next
))
180 queue_size (struct queue_t
*queue
)
191 queue_head (struct queue_t
*queue
)
193 if (!queue
|| !queue
->head
)
198 queue
->iterator
= queue
->head
;
200 return queue
->head
->item
;
204 queue_next (struct queue_t
*queue
)
206 if (!queue
|| !queue
->iterator
)
211 queue
->iterator
= queue
->iterator
->next
;
213 if (!queue
->iterator
)
218 return queue
->iterator
->item
;
222 queue_item (struct queue_t
*queue
)
224 if (!queue
|| !queue
->iterator
)
229 return queue
->iterator
->item
;
233 queue_print (char *name
, struct queue_t
*queue
, void (func
) (void *))
242 printf ("%s: ", name
);
250 fputs ("[ ", stdout
);
252 for (n
= queue
->head
; n
; n
= n
->next
)
261 fputs ("undef", stdout
);
267 printf ("] (%d items)\n", queue
->size
);