1
0
Fork 0
C_lib/stack/stack.h

65 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 file defines a stack (FIFO) structure and the associated function declarations. */
#ifndef STACK_H
#define STACK_H
/* Include stdlib. */
#include <stdlib.h>
/* Include assert.h to allow code generation, but only run with debug flag. */
#ifdef DEBUG
#ifndef NDEBUG
#define NDEBUG
#endif /* NDEBUG. */
#endif /* DEBUG. */
#include <assert.h>
/* Include the single node header. */
#include "../nodes/single_node.h"
/* Constants. */
#define INITIAL_STACK_SIZE 0
#define PUSH_SUCCESS 1
#define PUSH_FAIL 0
#define INVALID_SIZE -1
/* The actual stack struct. */
typedef struct stack {
/* Store the stack's total size. */
size_t size;
/* Stores the current last node. */
node_t *head;
} stack_t;
/* Function declarations. */
/* This function initializes a new empty stack. */
static inline stack_t* initialize_stack();
/* This function pops a node out. */
static inline node_t* pop(stack_t *stack);
/* This function "peeks" into the top node. */
static inline const void* peek(stack_t *stack);
/* This function pushes a node in. */
static inline void push(stack_t *stack, node_t *node);
/* This function creates a node, stores the data and pushes it in. */
static inline int push_data(stack_t *stack, void *data);
/* This function returns the stack's size. */
static inline size_t stack_size(stack_t *stack);
/* This function frees a stack, doen't touch the data. */
static inline void free_stack(stack_t **stack);
/* This function frees a stack and the data stored within. */
static inline void free_stack_data(stack_t **stack, void (*free_function)(const void*));
#endif /*STACK_H*/