stack.h (1915B)
1 #ifndef STACK_H 2 #define STACK_H 3 4 // stack 5 typedef struct Stack Stack; 6 7 /* 8 * stack_new: create a new stack 9 * returns: 10 * Stack * -> the created stack 11 * NULL -> failed to allocate memory for stack 12 */ 13 Stack *stack_new(void); 14 15 /* stack_delete: free the stack, but don't free any buffer in any of it's */ 16 void stack_delete(Stack *stack); 17 18 /* 19 * stack_free: free the stack, each element is also freed accordingly using 20 * 'free_data' 21 * parameters: 22 * stack (Stack *) -> stack to delete 23 * free_data -> function that would free resources of each element in stack 24 */ 25 void stack_free(Stack *stack, void (*free_data)(void *data)); 26 27 /* 28 * stack_push: push element to top of stack 29 * parameters: 30 * stack (Stack *) -> stack to push data into 31 * data (void *) -> data to push 32 * returns: 33 * void * -> the pushed data 34 * NULL -> failure of allocating memory for new stack element 35 */ 36 void *stack_push(Stack *stack, void *data); 37 38 /* stack_pop: pop element from top of stack */ 39 void *stack_pop(Stack *stack); 40 41 /* stack_get_size: get size of stack */ 42 size_t stack_size(Stack *stack); 43 44 /* 45 * stack_get_size: iterate over a stack top-to-bottom, running 'apply' on each 46 * stack element 47 * parameters: 48 * stack (Stack *) -> stack to iterate over 49 * apply -> function that would be passed each element in stack 50 * returns: 51 * void * -> sucess (pointer to stack) 52 * NULL -> one of the executions of 'apply' returned NULL 53 */ 54 void *stack_iter(Stack *stack, void *(*apply)(void *data)); 55 56 /* 57 * stack_iter_reverse: iterate over a stack bottom-to-top, running 'apply' on 58 * each stack element 59 * parameters: 60 * stack (Stack *) -> stack to iterate over 61 * apply -> function that would be passed each element in stack 62 * returns: 63 * void * -> sucess (pointer to stack) 64 * NULL -> one of the executions of 'apply' returned NULL 65 */ 66 void *stack_iter_reverse(Stack *stack, void *(*apply)(void *data)); 67 68 #endif /* STACK_H */