-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.h
54 lines (47 loc) · 1.08 KB
/
queue.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
struct qentry_struct {
struct qentry_struct *next;
struct qentry_struct *prev;
};
struct queue_struct {
struct qentry_struct *head;
struct qentry_struct *tail;
};
void queue_init(struct queue_struct *);
void queue(struct queue_struct *, struct qentry_struct *);
struct qentry_struct *unqueue(struct queue_struct *);
void queue_init(struct queue_struct *q)
{
q->head = NULL;
q->tail = NULL;
}
void queue(struct queue_struct *q, struct qentry_struct *qe)
{
// Queue to tail
if (q->head == NULL){
q->head = qe;
q->tail = qe;
qe->next = NULL;
qe->prev = NULL;
return;
}
qe->prev = q->tail;
qe->next = NULL;
q->tail->next = qe;
q->tail = qe;
}
struct qentry_struct *unqueue(struct queue_struct *q)
{
struct qentry_struct *qe;
// Unqueue from head
if (q->head == NULL){
return(NULL);
}
qe = q->head;
q->head = qe->next;
if (q->head == NULL){
q->tail = NULL;
}
qe->next = NULL;
qe->prev = NULL;
return(qe);
}