This is a generic queue implementation. A queue is a FIFO (first in, first out) data structure that supports two main operations: adding data to the queue (enqueue
) and retrieving the least recently added item (dequeue
). Additionally, it should be possible to know the size
of a queue and whether it is_empty
.
The Queue
API is detailed below.
Fields:
Node*
first
-- the least recently added nodeNode*
last
-- the most recently added nodeunsigned int size
-- number of items in the queue
Methods (assume queue
is a Queue*
):
Queue* queue_create()
-- Creates a newQueue
and returns a pointer to it, orNULL
in case of failure.queue->free(Queue* queue)
-- Frees the givenqueue
.queue->is_empty(Queue* queue)
-- Returnstrue
if thequeue
is empty,false
otherwise.queue->enqueue(Queue* queue, void* data)
-- Adds thedata
to thequeue
. Returns 0 on success and -1 on failure.queue->dequeue(Queue* queue)
-- Removes and returns the least recently added item of thequeue
. ReturnsNULL
if the queue is null or empty.queue->contains(Queue* queue, void* item)
-- Returnstrue
if the queue contains the item, andfalse
otherwise.
For more details: