-
-
Notifications
You must be signed in to change notification settings - Fork 357
/
utility.h
107 lines (81 loc) · 1.94 KB
/
utility.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#ifndef UTILITY_H
#define UTILITY_H
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
struct stack {
void **data;
size_t top, capacity, size;
};
struct queue {
void **data;
size_t front, back, capacity;
};
struct stack get_stack(size_t size) {
struct stack stk;
stk.data = malloc(4 * size);
stk.capacity = 4;
stk.top = 0;
return stk;
}
bool stack_empty(struct stack *stk) {
return stk->top == 0;
}
void stack_push(struct stack *stk, void *element) {
if (stk->top == stk->capacity) {
stk->capacity *= 2;
stk->data = realloc(stk->data, stk->capacity * sizeof(stk->data[0]));
}
stk->data[stk->top++] = element;
}
void *stack_pop(struct stack *stk) {
if (stack_empty(stk)) {
return NULL;
}
return stk->data[--stk->top];
}
void free_stack(struct stack stk) {
free(stk.data);
}
struct queue get_queue(size_t size) {
struct queue q;
q.data = calloc(4, size);
q.front = 0;
q.back = 0;
q.capacity = 4;
return q;
}
bool queue_empty(struct queue *q) {
return q->front == q->back;
}
void queue_resize(struct queue *q) {
size_t size = sizeof(q->data[0]);
void **tmp = calloc((q->capacity * 2), size);
memcpy(tmp, q->data + q->front, (q->capacity - q->front) * size);
memcpy(tmp + q->capacity - q->front, q->data, (q->front - 1) * size);
free(q->data);
q->data = tmp;
q->back = q->capacity - 1;
q->front = 0;
q->capacity *= 2;
}
void enqueue(struct queue *q, void *element) {
if (q->front == (q->back + 1) % q->capacity) {
queue_resize(q);
}
q->data[q->back] = element;
q->back = (q->back + 1) % q->capacity;
}
void *dequeue(struct queue *q) {
if (queue_empty(q)) {
return NULL;
}
void *ret = q->data[q->front];
q->front = (q->front + 1) % q->capacity;
return ret;
}
void free_queue(struct queue q) {
free(q.data);
}
#endif //UTILITY_H