/* This file contains the function implementations for queues. */
/* Include guards + includes. */
#ifndef QUEUE_C
#define QUEUE_C
#include"queue.h"
#endif /* QUEUE_C */
/* Function implementations. */
/* This function initializes a new empty queue. */
/* Will return a pointer to the queue on success, otherwise will return NULL. */
queue_t*initialize_queue(){
/* Variables. */
queue_t*result;
/* Attempt to allocate. */
result=(queue_t*)malloc(sizeof(queue_t));
/* Check if the allocation failed. */
if(result==NULL)
returnNULL;
/* Allocate the internal list struct. */
result->list=(list_t*)malloc(sizeof(list_t));
/* Check if the allocation failed. */
if(result->list==NULL){
free(result);
returnNULL;
}
/* Initialize the internal fields. */
result->tail=NULL;
result->list->head=NULL;
result->list->length=0;
/* Return the result. */
returnresult;
}
/* This function enqueues a node. */
/* This function attempts to enqueue a given node into the given queue, on success will return SUCCESS_CODE_QUEUE_C, otherwise will return FAILURE_CODE_QUEUE_C. */
/* Note that if either the queue or node pointer are NULL, the function will automatically fail. */
/* This function allocates and enqueues a node. */
/* This function attempts to create a node then enqueue it into the given queue, on success will return SUCCESS_CODE_QUEUE_C, otherwise will return FAILURE_CODE_QUEUE_C. */
/* Note that if either the queue pointer is NULL or node allocation fails, the function will automatically fail. */
/* Note that it'll return NULL on allocation failure or invalid input - in the former case it'll set the correct length in the specified pointer, in the second it will set -1. */