-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
64 lines (58 loc) · 1.07 KB
/
Copy pathqueue.c
File metadata and controls
64 lines (58 loc) · 1.07 KB
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
#include "queue.h"
#include <stdlib.h>
queue_t init_queue(void)
{
queue_t q;
q.head = NULL;
q.tail = NULL;
q.count = 0;
return q;
}
void delete_queue(queue_t *q)
{
while (q->head) {
node_t *tmp = q->head;
q->head = q->head->next;
free(tmp);
}
q->tail = NULL;
q->count = 0;
}
void enqueue(queue_t *q, node_t *n)
{
n->next = NULL;
if (q->head == NULL) {
q->head = n;
q->tail = n;
} else {
q->tail->next = n;
q->tail = n;
}
q->count++;
}
node_t *dequeue(queue_t *q)
{
if (q->head == NULL) {
return NULL;
}
node_t *n = q->head;
q->head = q->head->next;
q->count--;
return n;
}
void destroy_bfs_queue(queue_t *q)
{
while (q->head) {
node_t *current = q->head;
while (current->parent != NULL) {
node_t *tmp = current->parent;
current = tmp->parent;
free(tmp);
}
node_t *tmp = q->head;
q->head = q->head->next;
free(tmp);
}
q->tail = NULL;
q->count = 0;
}