58 lines
1.9 KiB
C
58 lines
1.9 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 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. */
|
|
int size;
|
|
/* Stores the current last node. */
|
|
node_t *head;
|
|
} stack_t;
|
|
|
|
/* Function declarations. */
|
|
/* This function initializes a new empty stack. */
|
|
stack_t* initialize_stack();
|
|
/* This function pops a node out. */
|
|
node_t* pop(stack_t *stack);
|
|
/* This function "peeks" into the top node. */
|
|
const void* peek(stack_t *stack);
|
|
/* This function pushes a node in. */
|
|
int push(stack_t *stack, node_t *node);
|
|
/* This function creates a node, stores the data and pushes it in. */
|
|
int push_data(stack_t *stack, void *data);
|
|
/* This function returns the stack's size. */
|
|
int stack_size(stack_t *stack);
|
|
/* This function frees a stack, doen't touch the data. */
|
|
void free_stack(stack_t **stack);
|
|
/* This function frees a stack and the data stored within. */
|
|
void free_stack_data(stack_t **stack, void (*free_function)(const void*));
|
|
|
|
#endif /*STACK_H*/
|