-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.h
83 lines (69 loc) · 1.53 KB
/
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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include <string.h>
#define DEFAULTSIZE 4
typedef struct {
void *elems;
int loglen;
int elemSize;
int alloclen;
void (*freefn)(void *);
} Stack;
void StackNew(Stack *, int, void (*)(void *));
void StackDispose(Stack *);
void StackPush(Stack *, void *);
void StackPop(Stack *, void *);
static bool isFull(Stack *);
static void StackGrow(Stack *);
void StackNew(Stack *s, int elemSize, void (*freefn)(void *))
{
s->loglen = 0;
s->elemSize = elemSize;
s->alloclen = DEFAULTSIZE;
s->freefn = freefn;
s->elems = malloc(s->alloclen * s->elemSize);
assert(s->elems != NULL);
}
void StackDispose(Stack *s)
{
if (s->freefn != NULL) {
int i; for (i = 0; i < s->loglen; i++) {
s->freefn((char *) s->elems + (i * s->elemSize));
}
}
free(s->elems);
}
void StackPush(Stack *s, void *elemAddr)
{
if (isFull(s)) {
StackGrow(s);
}
void *targetAddr = (char *) s->elems + (s->loglen * s->elemSize);
memcpy(targetAddr, elemAddr, s->elemSize);
s->loglen += 1;
}
void StackPop(Stack *s, void *elemAddr)
{
assert(s->loglen > 0);
s->loglen -= 1;
void *sourceAddr = (char *) s->elems + (s->loglen * s->elemSize);
memcpy(elemAddr, sourceAddr, s->elemSize);
}
int StackSize(Stack *s)
{
return s->loglen;
}
static bool isFull(Stack *s)
{
if (s->loglen == s->alloclen) {
return true;
}
return false;
}
static void StackGrow(Stack *s)
{
s->alloclen *= 2;
s->elems = realloc(s->elems, s->alloclen * s->elemSize);
assert(s->elems != NULL);
}