-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin_max_stack.h
30 lines (22 loc) · 964 Bytes
/
min_max_stack.h
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
// A dynamically-resized stack with O(1) min/max functions. Tries to be
// reasonably space efficient by only storing a new element into the min/max
// stacks whenever the min/max changes.
typedef struct Min_max_stack {
Stack val_stack;
Stack min_stack;
Stack max_stack;
} Min_max_stack;
void min_max_stack_init(Min_max_stack *stack);
void min_max_stack_free(Min_max_stack *stack);
// Returns the number of elements stored in 'stack'
size_t min_max_stack_len(Min_max_stack *stack);
// Pushes a new element onto 'stack'
void min_max_stack_push(Min_max_stack *stack, int val);
// Returns the top element of 'stack' without modifying 'stack'
int min_max_stack_peek(Min_max_stack *stack);
// Pops the top element off 'stack'
int min_max_stack_pop(Min_max_stack *stack);
// Returns the maximum element in the stack
int min_max_stack_min(Min_max_stack *stack);
// Returns the minimum element in the stack
int min_max_stack_max(Min_max_stack *stack);