summaryrefslogtreecommitdiff
path: root/stack.h
blob: 611a54c4022d615b2c39730534002b43e61ab8e9 (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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 */