1
0
Fork 0
C_lib/linked_lists/queue.h

61 lines
2.2 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"
/* 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. */
single_linked_list_t *list;
/* Keep the tail for fast access. */
node_t *tail;
} queue_t;
/* This function initializes a new queue. */
queue_t *initialize_queue();
/* This function enqueues a node. */
int enqueue(queue_t *queue, node_t *node);
/* This function creates and enqueues a node. */
int enqueue_data(queue_t *queue, void *data);
/* This function dequeues the last node. */
node_t *dequeue(queue_t *queue);
/* This function deletes all the nodes of the given queue. */
int clear_queue(queue_t **queue);
/* This function deletes all the nodes of the given queue. */
int clear_queue_data(queue_t **queue, void (*free_function)());
/* This function serializes the queue. */
void **serialize_queue(queue_t *queue, int *length);
#endif /* QUEUE_H */