-
Notifications
You must be signed in to change notification settings - Fork 0
/
_stack.c
54 lines (44 loc) · 1.03 KB
/
_stack.c
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
#ifdef T
#include "generic.h"
#include "_stack.h"
#include <stdlib.h>
stack(T) *stack_alloc(T)(void) {
return calloc(1, sizeof(stack(T)));
}
void stack_init(T)(stack(T) *s) {
s->index = -1;
s->block = NULL;
}
stack(T) *stack_make(T)(void) {
stack(T) *s = stack_alloc(T)();
stack_init(T)(s);
return s;
}
void stack_free(T)(stack(T) *s) {
free(s);
}
T stack_pop(T)(stack(T) *s) {
if (s->index == -1) return 0;
T value = s->block->elements[s->index--];
if (s->index < 0 && s->block->prev_block) {
s->block = s->block->prev_block;
s->index = STACK_BLOCK_SIZE-1;
}
return value;
}
void stack_push(T)(stack(T) *s, T value) {
if (s->block == NULL) {
s->block = calloc(1, sizeof(*s->block));
}
if (s->index == STACK_BLOCK_SIZE-1) {
s->block->next_block = calloc(1, sizeof(*s->block));
s->block->next_block->prev_block = s->block;
s->block = s->block->next_block;
s->index = -1;
}
s->block->elements[++s->index] = value;
}
bool stack_is_empty(T)(stack(T) *s) {
return s->index == -1;
}
#endif