]>
git.datanom.net - clib.git/blob - src/queue.c
4 typedef struct _Queue
{
10 static void free_elem(Queue
* queue
) {
12 for (int i
= 0; i
< queue
->size
; i
++)
13 queue
->free(queue
->elems
[i
]);
19 Queue
* q
= malloc(sizeof(Queue
));
26 Queue
* queue_new_full(QueueFreeFunc free
){
27 Queue
* q
= malloc(sizeof(Queue
));
34 void queue_destroy(Queue
* queue
) {
40 static void extend(Queue* queue, void* elem) {
41 while (int i = queue->size - 1; i > 0; i--) {
42 queue->elems[i] = queue->elems[i - 1]
47 void* queue_dequeue(Queue
* queue
) {
48 if (queue_empty(queue
))
51 void* elem
= queue
->elems
[queue
->size
];
52 queue
->elems
[queue
->size
] = NULL
;
53 queue
->elems
= realloc(queue
->elems
, queue
->size
* sizeof(void*));
58 void queue_enqueue(Queue
* queue
, void* elem
) {
63 queue
->elems
= realloc(queue
->elems
, queue
->size
* sizeof(void*));
64 for (int i
= queue
->size
- 1; i
> 0; i
--) {
65 queue
->elems
[i
] = queue
->elems
[i
- 1];
67 queue
->elems
[0] = elem
;
70 void* queue_front(Queue
* queue
) {
71 if (queue_empty(queue
))
73 return queue
->elems
[0];
76 void* queue_back(Queue
* queue
) {
77 if (queue_empty(queue
))
79 return queue
->elems
[queue
->size
- 1];
82 bool queue_empty(Queue
* queue
) {
83 return (queue
== NULL
|| queue
->size
== 0);
86 void queue_clear(Queue
* queue
) {
95 int queue_size(Queue
* queue
) {
96 if (queue_empty(queue
))
This page took 0.099753 seconds and 6 git commands to generate.