]>
wirehaze git hosting - ppos.git/blob - queue/queue.c
2aed9eb54cae7f70b4021011226bf4b40adeba7c
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
;
67 queue_add (struct queue_t
*queue
, void *item
)
74 if (!(n
= node_create ()))
82 queue
->head
= queue
->tail
= queue
->iterator
= n
;
86 queue
->tail
= queue
->tail
->next
= n
;
91 queue_del (struct queue_t
*queue
, void *item
)
93 struct node_t
*n
, *prev
;
98 if (!(n
= queue
->head
))
103 while (n
&& n
->item
!= item
)
109 if (!n
) /* not found */
113 prev
->next
= n
->next
;
115 if (queue
->head
== n
)
116 queue
->head
= n
->next
;
118 if (queue
->tail
== n
)
121 if (queue
->iterator
== n
)
122 queue
->iterator
= n
->next
;
130 queue_has (struct queue_t
*queue
, void *item
)
137 if (!(n
= queue
->head
))
140 while (n
->item
!= item
&& (n
= n
->next
))
147 queue_size (struct queue_t
*queue
)
156 queue_head (struct queue_t
*queue
)
158 if (!queue
|| !queue
->head
)
161 queue
->iterator
= queue
->head
;
163 return queue
->head
->item
;
167 queue_next (struct queue_t
*queue
)
169 if (!queue
|| !queue
->iterator
)
172 queue
->iterator
= queue
->iterator
->next
;
174 if (!queue
->iterator
)
177 return queue
->iterator
->item
;
181 queue_item (struct queue_t
*queue
)
183 if (!queue
|| !queue
->iterator
)
186 return queue
->iterator
->item
;
190 queue_print (char *name
, struct queue_t
*queue
, void (func
) (void *))
197 printf ("%s: ", name
);
205 fputs ("[ ", stdout
);
207 for (n
= queue
->head
; n
; n
= n
->next
)
213 fputs ("undef", stdout
);
218 printf ("] (%d items)\n", queue
->size
);