Queue
A queue stores elements in first-in-first-out order, each slot is exactly elesz bytes, enqueue adds at the back, dequeue removes from the front, peek reads the front without removing, initialization, destructor behavior, and error conventions follow the same pattern as deq_init and related deque APIs.
Header
Section titled “Header”#include <queue.h>Struct
Section titled “Struct”struct queue { struct deque deq;};deq holds the queue state, including element size and the optional destructor given to queue_init.
Macros
Section titled “Macros”queue_empty
Section titled “queue_empty”queue_empty(queue)Returns non-zero if the queue contains no elements.
Parameters
queue— pointer to the queue
queue_size
Section titled “queue_size”queue_size(queue)Returns the current element count.
Parameters
queue— pointer to the queue
Functions
Section titled “Functions”queue_init
Section titled “queue_init”int queue_init(struct queue *queue, size_t elesz, void (*destroy)(void *));Initializes an empty queue with element size elesz and an optional destructor. Must be called before any other function. Returns 0 on success, -1 on error.
Parameters
queue— pointer to an uninitialized queue structelesz— byte size of each element, must be non-zerodestroy— called on each element when it is discarded, or NULL for no-op
queue_fini
Section titled “queue_fini”void queue_fini(struct queue *queue);Destroys all elements and frees the buffer. No-op if queue is NULL. Does not free the struct queue itself.
Parameters
queue— pointer to the queue
queue_enq
Section titled “queue_enq”int queue_enq(struct queue *queue, void *ele);Copies elesz bytes from ele into a new slot at the back, growing the buffer if needed. Returns 0 on success, -1 on error.
Parameters
queue— pointer to the queueele— pointer to the value to copy, must not be NULL
queue_deq
Section titled “queue_deq”int queue_deq(struct queue *queue, void *dest);Removes the front element. If dest is non-NULL, copies the element there and skips destroy. If dest is NULL and destroy is set, calls destroy on the element. Returns 0 on success, -1 if the queue is empty or queue is NULL.
Parameters
queue— pointer to the queuedest— destination buffer of at leasteleszbytes to receive the element, or NULL to invokedestroy
queue_peek
Section titled “queue_peek”void *queue_peek(struct queue *queue);Returns a pointer to the front element, or NULL if the queue is empty or queue is NULL.
Parameters
queue— pointer to the queue
queue_clear
Section titled “queue_clear”void queue_clear(struct queue *queue);Removes all elements and resets size to zero, calling destroy on each when set. Does not free the buffer or the struct queue itself. No-op if queue is NULL.
Parameters
queue— pointer to the queue
Example
Section titled “Example”#include <queue.h>
int main(void){ struct queue q; int x, y;
if (queue_init(&q, sizeof(int), NULL) != 0) return 1;
x = 10; queue_enq(&q, &x); x = 20; queue_enq(&q, &x);
if (!queue_empty(&q)) y = *(int *)queue_peek(&q); /* oldest value 10 */
queue_deq(&q, &y); /* y == 10 */ queue_deq(&q, &y); /* y == 20 */
queue_clear(&q); queue_fini(&q); return 0;}