The LevAWC Project
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Pages
Functions
queue.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"

Go to the source code of this file.

Functions

Queue QUEUEinit (void(*destroy)(void *data))
 
void QUEUEdestroy (Queue que)
 
int QUEUEenqueue (Queue que, const void *data)
 
int QUEUEdequeue (Queue que, void **data)
 
void * QUEUEpeek (Queue que)
 
int QUEUEisempty (Queue que)
 
int QUEUEsize (Queue que)
 

Function Documentation

int QUEUEdequeue ( Queue  queue,
void **  data 
)

Remove(=dequeue) the head element.

When called, the 2nd parameter of this function, data, should reference an (external, user-defined) pointer. After the call - this referenced, external pointer has been redirected, to point to the data of the removed element - if the call was succesful. The caller is responsible for the future of this memory - deallocating it, if needed, for example.

Parameters
[in]queue- reference to current queue.
[out]data- reference to a pointer. After the call, this referenced pointer has been redirected to point to the data of the removed element - if the call was successful. The caller is responsible for the future of this memory - deallocating it, for example.
Returns
Value 0 - if the call was OK - or value -1 otherwise.

Definition at line 47 of file queue.c.

void QUEUEdestroy ( Queue  queue)

Destroy the queue.

The queue is destroyed - that is, all memory occupied by the elements is deallocated. The user-defined callback function destroy, given as an argument to QUEUEinit(), is responsible for freeing dynamically allocated element data, when this function is called. When all elements and data have been deallocated - the queue header is deallocated, too.

Parameters
[in]queue- a reference to current queue.
Returns
Nothing.
See Also
QUEUEinit()

Definition at line 37 of file queue.c.

int QUEUEenqueue ( Queue  queue,
const void *  data 
)

Insert(=enqueue) a new element - at the end of the queue.

This function inserts an new element - with a reference to its corresponding data, given by parameter data - at the end of the queue.

Parameters
[in]queue- reference to current queue
[in]data- reference to data to be stored in the new element, which is to be inserted at the end of the queue.
Returns
Value 0 - if everything went OK - or value -1 otherwise.

Definition at line 42 of file queue.c.

Queue QUEUEinit ( void(*)(void *data)  destroy)

Initiate the queue.

Parameters
[in]destroy- A reference to a user-made function, reponsible for freeing element data, when the queue is deleted. If destroy is NULL - then element data will be left untouched when the list is destroyed.
Returns
A reference - to a new, empty queue - if dynamic memory allocation for the ADT was successful - or NULL otherwise. Take really good care of this return value, since it will be needed as a parameter in subsequent calls - to the majority of other queue handling functions in this queue function interface - i.e. a sort of "handle" to the queue.
See Also
QUEUEdestroy()

Definition at line 32 of file queue.c.

int QUEUEisempty ( Queue  queue)

Determine if the queue is empty - or not.

Parameters
[in]queue- a reference to the current queue.
Returns
Value 1 - if the queue is indeed empty - or 0 otherwise.

Definition at line 57 of file queue.c.

void* QUEUEpeek ( Queue  queue)

Peek at the head of the queue.

Parameters
[in]queue- reference to the current queue.
Returns
NULL if the queue is empty - or a reference to data of the head element, otherwise.

Definition at line 52 of file queue.c.

int QUEUEsize ( Queue  queue)

Get the queue size.

Parameters
[in]queue- a reference to the current queue.
Returns
The size, that is, the number of elements in the queue.

Definition at line 62 of file queue.c.