From b7ac144cd2d242791938b51569effb7a1378a332 Mon Sep 17 00:00:00 2001 From: noodle Date: Mon, 10 Jul 2023 15:40:08 +0300 Subject: Add files --- stack.h | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 stack.h (limited to 'stack.h') diff --git a/stack.h b/stack.h new file mode 100644 index 0000000..611a54c --- /dev/null +++ b/stack.h @@ -0,0 +1,68 @@ +#ifndef STACK_H +#define STACK_H + +// stack +typedef struct Stack Stack; + +/* + * stack_new: create a new stack + * returns: + * Stack * -> the created stack + * NULL -> failed to allocate memory for stack + */ +Stack *stack_new(void); + +/* stack_delete: free the stack, but don't free any buffer in any of it's */ +void stack_delete(Stack *stack); + +/* + * stack_free: free the stack, each element is also freed accordingly using + * 'free_data' + * parameters: + * stack (Stack *) -> stack to delete + * free_data -> function that would free resources of each element in stack + */ +void stack_free(Stack *stack, void (*free_data)(void *data)); + +/* + * stack_push: push element to top of stack + * parameters: + * stack (Stack *) -> stack to push data into + * data (void *) -> data to push + * returns: + * void * -> the pushed data + * NULL -> failure of allocating memory for new stack element + */ +void *stack_push(Stack *stack, void *data); + +/* stack_pop: pop element from top of stack */ +void *stack_pop(Stack *stack); + +/* stack_get_size: get size of stack */ +size_t stack_size(Stack *stack); + +/* + * stack_get_size: iterate over a stack top-to-bottom, running 'apply' on each + * stack element + * parameters: + * stack (Stack *) -> stack to iterate over + * apply -> function that would be passed each element in stack + * returns: + * void * -> sucess (pointer to stack) + * NULL -> one of the executions of 'apply' returned NULL + */ +void *stack_iter(Stack *stack, void *(*apply)(void *data)); + +/* + * stack_iter_reverse: iterate over a stack bottom-to-top, running 'apply' on + * each stack element + * parameters: + * stack (Stack *) -> stack to iterate over + * apply -> function that would be passed each element in stack + * returns: + * void * -> sucess (pointer to stack) + * NULL -> one of the executions of 'apply' returned NULL + */ +void *stack_iter_reverse(Stack *stack, void *(*apply)(void *data)); + +#endif /* STACK_H */ -- cgit v1.2.3