-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
62 lines (49 loc) · 1.36 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
55
56
57
58
59
60
61
62
#include "stack.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
static void stack_double(stack *s) {
s->allocated *= 2;
s->data = realloc(s->data, s->allocated * s->elem_size);
assert(s->data != NULL);
}
void stack_init(stack *s, size_t elem_size, void (*dispose)(void *)) {
s->size = 0;
s->allocated = 4;
s->elem_size = elem_size;
s->dispose = dispose;
s->data = malloc(s->allocated * s->elem_size);
assert(s->data != NULL);
}
void stack_dispose(stack *s) {
if (s->dispose != 0) {
void *elem = s->data;
for (size_t i = 0; i < s->size; i++) {
s->dispose(elem);
elem = (char *)elem + s->elem_size;
}
}
free(s->data);
}
void stack_push(stack *s, void *elem_addr) {
if (s->size == s->allocated) {
stack_double(s);
}
void *elem = (char *)s->data + s->size * s->elem_size;
memcpy(elem, elem_addr, s->elem_size);
s->size++;
}
void stack_pop(stack *s, void *elem_addr) {
assert(s->size > 0);
s->size--;
void *elem = (char *)s->data + s->size * s->elem_size;
memcpy(elem_addr, elem, s->elem_size);
}
void stack_peek(stack *s, void *elem_addr) {
assert(s->size > 0);
void *elem = (char *)s->data + (s->size - 1) * s->elem_size;
memcpy(elem_addr, elem, s->elem_size);
}
size_t stack_size(stack *s) {
return s->size;
}