1
0
Fork 0
C_lib/linked_lists/queue.h

72 lines
3.0 KiB
C

/*
* C_lib Copyright (C) 2021 Wael Karram.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/* This header defines a queue and related functions. */
/* Rely internally on a single linked list. */
#ifndef QUEUE_H
#define QUEUE_H
#include "single_linked_list.h"
/* Debug definitions. */
#ifdef DEBUG
#ifndef NDEBUG
#define NDEBUG
#endif /* NDEBUG. */
#endif /* DEBUG. */
#include <assert.h>
/* Constants. */
/* Function error/return code on success. */
#define SUCCESS_CODE_QUEUE_C 1
/* Function error/return code on failure. */
#define FAILURE_CODE_QUEUE_C 0
/* Function error/return code on failure to find index. */
#define INDEX_NOT_FOUND_CODE_QUEUE_C -1
/* Function error/return code on invalid input. */
#define INVALID_INPUT_CODE_QUEUE_C -2
/* Error code returned when input fails sanity check. */
#define CODE_FREE_SUCCESS_MALFORMED_QUEUE_C -1
/* The default index of the queue's head. */
#define INDEX_HEAD_QUEUE_C 0
/* The linked list's struct. */
typedef struct queue {
/* Single linked list as the backbone. */
linked_list_t *list;
/* Keep the tail for fast access. */
node_t *tail;
} queue_t;
/* This function initializes a new queue. */
static inline queue_t *initialize_queue();
/* This function enqueues a node. */
static inline void enqueue(queue_t *queue, node_t *node);
/* This function creates and enqueues a node. */
static inline int enqueue_data(queue_t *queue, void *data);
/* This function dequeues the last node. */
static inline node_t *dequeue(queue_t *queue);
/* This function deletes all the nodes of the given queue, including the queue itself. */
static inline void clear_queue(queue_t **queue);
/* This function deletes all the nodes of the given queue, including the stored data and the queue itself. */
static inline void clear_queue_data(queue_t **queue, void (*free_function)(const void*));
/* This function deletes all the nodes of the given queue, but doesn't touch the queue struct itself. */
static inline void clear_queue_nodes(queue_t *queue);
/* This function deletes all the nodes of the given queue - including the stored data - but doesn't touch the queue struct itself. */
static inline void clear_queue_nodes_data(queue_t *queue, void (*free_function)(const void*));
/* This function serializes the queue into an array, it relies on an array setting function and an allocated buffer. */
static inline void *serialize_queue(queue_t *queue, void (*array_setting_function)(void*, size_t, void*), void *array);
#endif /* QUEUE_H */