5b24a3db976a02d45570cabde2043e45e0a76307
5 #include "cgds/Queue.h"
7 void _queue_init(Queue
* queue
, size_t dataSize
)
10 queue
->dataSize
= dataSize
;
15 Queue
* _queue_new(size_t dataSize
)
17 Queue
* queue
= (Queue
*) safe_malloc(sizeof (Queue
));
18 _queue_init(queue
, dataSize
);
22 Queue
* queue_copy(Queue
* queue
)
24 Queue
* queueCopy
= _queue_new(queue
->dataSize
);
25 QueueCell
* queueCell
= queue
->front
;
26 while (queueCell
!= NULL
)
28 _queue_push(queueCopy
, queueCell
->data
);
29 queueCell
= queueCell
->next
;
34 Bool
queue_empty(Queue
* queue
)
36 return (queue
->size
== 0);
39 UInt
queue_size(Queue
* queue
)
44 void _queue_push(Queue
* queue
, void* data
)
46 QueueCell
* newQueueBack
= (QueueCell
*) safe_malloc(sizeof (QueueCell
));
47 newQueueBack
->data
= safe_malloc(queue
->dataSize
);
48 memcpy(newQueueBack
->data
, data
, queue
->dataSize
);
49 newQueueBack
->next
= NULL
;
51 queue
->back
->next
= newQueueBack
;
52 queue
->back
= newQueueBack
;
54 queue
->front
= newQueueBack
;
58 void* _queue_peek(Queue
* queue
)
60 return queue
->front
->data
;
63 void queue_pop(Queue
* queue
)
65 QueueCell
* toTrash
= queue
->front
;
66 queue
->front
= queue
->front
->next
;
67 if (queue
->front
== NULL
)
69 safe_free(toTrash
->data
);
74 void queue_clear(Queue
* queue
)
76 while (queue
->size
> 0)
78 _queue_init(queue
, queue
->dataSize
);
81 void queue_destroy(Queue
* queue
)