-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_struct_queue.c
56 lines (49 loc) · 1.05 KB
/
mqtt_struct_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
#include <stdio.h>
#include <stdlib.h>
// Code auf Basis von Youtube Tutorial
// https://www.youtube.com/watch?time_continue=543&v=yKNPFKfnlt8
// https://www.geeksforgeeks.org/queue-linked-list-implementation/
typedef struct queueElem {
char *topic, *value;
struct queueElem *next;
} queueElem;
typedef struct Queue {
int elements;
queueElem * head, * tail;
} Queue;
Queue * initQueue() {
Queue * Q = malloc(sizeof(Queue));
Q->elements = 0;
Q->head = Q->tail = NULL;
return Q;
}
void enqueue(Queue * Q, char *topic, char *value) {
queueElem *el = malloc(sizeof(queueElem));
el->topic = topic;
el->value = value;
el->next = NULL;
if(Q->tail == NULL) {
Q->head = Q->tail = el;
}
else {
Q->tail->next = Q->tail = el;
}
Q->elements++;
}
void dequeue(Queue * Q, char **topic, char **value) {
if(Q->head == NULL) {
return;
}
queueElem * out = Q->head;
Q->head = Q->head->next;
if(Q->head == NULL) {
Q->tail = NULL;
}
*topic = out->topic;
*value = out->value;
free(out);
Q->elements--;
}
int getElementCount(Queue * Q) {
return Q->elements;
}