Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#ifndef BARRUST_STACK_H__
#define BARRUST_STACK_H__
/*******************************************************************************
***
*** Author: Tyler Barrus
*** email: barrust@gmail.com
***
*** Version: 0.1.0
*** Purpose: Generic stack (last in first out [LIFO]) implementation
***
*** License: MIT 2020
***
*** URL: https://github.com/barrust/c-utils
***
*** Usage:
***
***
*******************************************************************************/
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct __linked_list stack;
typedef struct __linked_list *stack_list_t;
#define STACK_SUCCESS 0
#define STACK_FAILURE -1
typedef struct __stack_node {
void* data;
struct __stack_node* next;
} stack_node;
/* Initialize the singly linked list
Returns:
NULL - If error allocating the memory
stack_list_t
*/
stack_list_t stk_init(void);
/* Free the data from the singly linked list;
NOTE: does not free the data element */
void stk_free(stack_list_t stk);
/* Free the data from the singly linked list;
NOTE: to free the data, set free_data to true */
void stk_free_alt(stack_list_t stk, bool free_data);
/* Returns the number of nodes in the doubly linked list */
size_t stk_num_elements(stack_list_t stk);
/* Return the head node from the singly linked list */
stack_node* stk_first_node(stack_list_t stk);
/* Insert a new node into the queue */
int stk_push(stack_list_t stk, void* data);
/* Pop the node from the front of the queue */
void* stk_pop(stack_list_t stk);
void stk_pop_alt(stack_list_t stk, bool free_data);
/* Traverse the list easily using the following macros */
#define stk_traverse(stk, node) for (node = stk_first_node(stk); node != NULL; node = node->next)
#ifdef __cplusplus
} // extern "C"
#endif
#endif