-
Notifications
You must be signed in to change notification settings - Fork 1
/
queue.c
96 lines (73 loc) · 1.32 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
/*
ssid-logger is a simple software to log SSID you encounter in your vicinity
Copyright © 2020-2022 solsTiCe d'Hiver
*/
#include <stdlib.h>
#include "queue.h"
queue_t *new_queue(int capacity)
{
queue_t *q;
q = malloc(sizeof(queue_t));
if (q == NULL) {
return q;
}
q->size = 0;
q->max_size = capacity;
q->head = NULL;
q->tail = NULL;
return q;
}
int enqueue(queue_t * q, void *value)
{
if ((q->size + 1) > q->max_size) {
return q->size;
}
struct Node *node = malloc(sizeof(struct Node));
if (node == NULL) {
return q->size;
}
node->value = value;
node->next = NULL;
if (q->head == NULL) {
q->head = node;
q->tail = node;
q->size = 1;
return q->size;
}
q->tail->next = node;
q->tail = node;
q->size += 1;
return q->size;
}
void *dequeue(queue_t * q)
{
if (q->size == 0) {
return NULL;
}
void *value = NULL;
struct Node *tmp = NULL;
value = q->head->value;
tmp = q->head;
q->head = q->head->next;
q->size -= 1;
free(tmp);
return value;
}
void free_queue(queue_t * q)
{
if (q == NULL) {
return;
}
while (q->head != NULL) {
struct Node *tmp = q->head;
q->head = q->head->next;
if (tmp->value != NULL) {
free(tmp->value);
}
free(tmp);
}
if (q->tail != NULL) {
free(q->tail);
}
free(q);
}