-
Notifications
You must be signed in to change notification settings - Fork 3
/
queue.c
42 lines (40 loc) · 981 Bytes
/
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
#include "queue.h"
void enqueue (struct queue *queue, struct link *link, bool update) {
if (queue->last)
queue->last->next = link;
else
queue->first = link;
link->prev = queue->last;
queue->last = link;
link->next = 0;
link->stamp = ++queue->stamp;
if (update || !queue->search)
queue->search = link;
}
void dequeue (struct queue *queue, struct link *link) {
assert (queue->search);
if (link->prev) {
assert (link->prev->next == link);
link->prev->next = link->next;
} else {
assert (queue->first == link);
queue->first = link->next;
}
if (link->next) {
assert (link->next->prev == link);
link->next->prev = link->prev;
} else {
assert (queue->last == link);
queue->last = link->prev;
}
if (queue->search == link) {
if (link->next)
queue->search = link->next;
else if (link->prev)
queue->search = link->prev;
else
queue->search = 0;
}
link->prev = 0;
link->next = 0;
}