]>
wirehaze git hosting - ppos.git/blob - queue/queue.c
6a08e90d22045c54aa4d6325fe4f619e35547b27
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 */
13 /* ------------------------------------------------------------------------- */
14 /* Data types ------------------------------------------------------------- */
19 void *item
; /* User provided opaque pointer. */
20 struct node_t
*next
; /* Pointer to the next node in the queue. */
23 /* ------------------------------------------------------------------------- */
28 struct node_t
*head
; /* Pointer to the first node in the queue. */
29 struct node_t
*tail
; /* Pointer to the last node in the queue. */
30 struct node_t
*iterator
; /* Pointer to the current node. */
31 int size
; /* Number of nodes in the queue. */
34 /* ------------------------------------------------------------------------- */
35 /* Internal functions ----------------------------------------------------- */
37 static inline struct node_t
*
40 return calloc (1, sizeof (struct node_t
));
43 /* ------------------------------------------------------------------------- */
46 node_destroy (struct node_t
*node
)
51 /* ------------------------------------------------------------------------- */
52 /* Exported functions ----------------------------------------------------- */
57 return calloc (1, sizeof (struct queue_t
));
60 /* ------------------------------------------------------------------------- */
63 queue_destroy (struct queue_t
*queue
)
65 struct node_t
*n
, *next
;
74 /* Save the next node pointer before destroying the current node. */
84 /* ------------------------------------------------------------------------- */
87 queue_add (struct queue_t
*queue
, void *item
)
94 if (!(n
= node_create ()))
100 /* Empty queue, this is the first element. */
103 queue
->head
= queue
->tail
= queue
->iterator
= n
;
107 /* Append to queue. */
108 queue
->tail
= queue
->tail
->next
= n
;
112 /* ------------------------------------------------------------------------- */
115 queue_del (struct queue_t
*queue
, void *item
)
117 struct node_t
*n
, *prev
;
122 if (!(n
= queue
->head
)) /* Queue is empty. */
127 while (n
&& n
->item
!= item
)
133 if (!n
) /* Not found. */
137 prev
->next
= n
->next
;
139 /* Update boundaries accordingly. */
140 if (queue
->head
== n
)
141 queue
->head
= n
->next
;
143 if (queue
->tail
== n
)
146 /* Advance the iterator if it points to the removed node. */
147 if (queue
->iterator
== n
)
148 queue
->iterator
= n
->next
;
155 /* ------------------------------------------------------------------------- */
158 queue_has (struct queue_t
*queue
, void *item
)
165 if (!(n
= queue
->head
))
168 /* Traverse until the item is found or the end is reached. */
169 while (n
->item
!= item
&& (n
= n
->next
))
175 /* ------------------------------------------------------------------------- */
178 queue_size (struct queue_t
*queue
)
186 /* ------------------------------------------------------------------------- */
189 queue_head (struct queue_t
*queue
)
191 if (!queue
|| !queue
->head
)
194 queue
->iterator
= queue
->head
;
196 return queue
->head
->item
;
199 /* ------------------------------------------------------------------------- */
202 queue_next (struct queue_t
*queue
)
204 if (!queue
|| !queue
->iterator
)
207 queue
->iterator
= queue
->iterator
->next
;
209 if (!queue
->iterator
)
212 return queue
->iterator
->item
;
215 /* ------------------------------------------------------------------------- */
218 queue_item (struct queue_t
*queue
)
220 if (!queue
|| !queue
->iterator
)
223 return queue
->iterator
->item
;
226 /* ------------------------------------------------------------------------- */
229 queue_print (char *name
, struct queue_t
*queue
, void (func
) (void *))
236 printf ("%s: ", name
);
244 fputs ("[ ", stdout
);
246 for (n
= queue
->head
; n
; n
= n
->next
)
252 fputs ("undef", stdout
);
257 printf ("] (%d items)\n", queue
->size
);
260 /* ------------------------------------------------------------------------- */