-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.c
114 lines (99 loc) · 2.18 KB
/
queue.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
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
108
109
110
111
112
113
114
#include <stdlib.h>
// FIFO
struct el {
struct el *next;
void *value;
};
struct queue {
int count;
struct el *head;
struct el *tail;
};
struct queue *queue_new() {
struct queue *queue = malloc(sizeof(struct queue));
queue->head = NULL;
queue->tail = NULL;
queue->count = 0;
return queue;
}
void queue_add(struct queue *queue, void *value) {
struct el *node = malloc(sizeof(struct el));
node->next = NULL;
node->value = value;
if (queue->tail) {
queue->tail->next = node;
queue->tail = node;
} else {
queue->head = node;
queue->tail = node;
}
queue->count++;
}
void *queue_peek(struct queue *queue) {
return queue->head->value;
}
void *queue_take(struct queue *queue) {
struct el *head = queue->head;
if (!queue->head) {
return NULL;
}
void *value = head->value;
queue->head = head->next;
if (queue->head == NULL) {
queue->tail = NULL;
}
free(head);
queue->count--;
return value;
}
int queue_count(struct queue *queue) {
return queue->count;
}
void queue_free(struct queue *queue) {
while (queue_take(queue)) {
// XXX leaks values?
}
free(queue);
}
struct array_queue {
void **arr;
int size;
int head;
int tail;
};
struct array_queue *array_queue_new(int size) {
struct array_queue *queue = malloc(sizeof(struct array_queue));
// adding 1 to avoid aliasing full and empty
queue->arr = calloc(size + 1, sizeof(void *));
queue->size = size + 1;
queue->head = 0;
queue->tail = 0;
return queue;
}
void array_queue_add(struct array_queue *queue, void *value) {
int size = queue->size;
queue->arr[queue->tail] = value;
queue->tail = (queue->tail + 1) % size;
}
void *array_queue_take(struct array_queue *queue) {
if (queue->head == queue->tail) {
return NULL;
}
int size = queue->size;
void *value = queue->arr[queue->head];
queue->head = (queue->head + 1) % size;
return value;
}
void *array_queue_peek(struct array_queue *queue) {
return (queue->head == queue->tail) ? NULL :
queue->arr[queue->head];
}
int array_queue_count(struct array_queue *queue) {
return queue->head > queue->tail ?
queue->size + queue->tail - queue->head :
queue->tail - queue->head;
}
void array_queue_free(struct array_queue *queue) {
free(queue->arr);
free(queue);
}