]>
wirehaze git hosting - ppos.git/blob - queue/queue.c
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
;
69 queue_add (struct queue_t
*queue
, void *item
)
78 if (!(n
= node_create ()))
88 queue
->head
= queue
->tail
= queue
->iterator
= n
;
92 queue
->tail
= queue
->tail
->next
= n
;
97 queue_del (struct queue_t
*queue
, void *item
)
99 struct node_t
*n
, *prev
;
106 if (!(n
= queue
->head
))
113 while (n
&& n
->item
!= item
)
119 if (!n
) /* not found */
126 prev
->next
= n
->next
;
129 if (queue
->head
== n
)
131 queue
->head
= n
->next
;
134 if (queue
->tail
== n
)
139 if (queue
->iterator
== n
)
141 queue
->iterator
= n
->next
;
150 queue_has (struct queue_t
*queue
, void *item
)
159 if (!(n
= queue
->head
))
164 while (n
->item
!= item
&& (n
= n
->next
))
171 queue_size (struct queue_t
*queue
)
182 queue_head (struct queue_t
*queue
)
184 if (!queue
|| !queue
->head
)
189 queue
->iterator
= queue
->head
;
191 return queue
->head
->item
;
195 queue_next (struct queue_t
*queue
)
197 if (!queue
|| !queue
->iterator
)
202 queue
->iterator
= queue
->iterator
->next
;
204 if (!queue
->iterator
)
209 return queue
->iterator
->item
;
213 queue_item (struct queue_t
*queue
)
215 if (!queue
|| !queue
->iterator
)
220 return queue
->iterator
->item
;
224 queue_print (char *name
, struct queue_t
*queue
, void (func
) (void *))
233 printf ("%s: ", name
);
241 fputs ("[ ", stdout
);
243 for (n
= queue
->head
; n
; n
= n
->next
)
252 fputs ("undef", stdout
);
258 printf ("] (%d items)\n", queue
->size
);