forked from cubzh/cubzh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilo_list_uint16.c
79 lines (63 loc) · 1.9 KB
/
filo_list_uint16.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
// -------------------------------------------------------------
// Cubzh Core
// filo_list_uint16.c
// Created by Gaetan de Villele on February 10, 2019.
// -------------------------------------------------------------
#include "filo_list_uint16.h"
#include <stdlib.h>
struct _FiloListUInt16 {
FiloListUInt16Node *first;
};
struct _FiloListUInt16Node {
FiloListUInt16Node *next; /* 8 bytes */
// stored integer
uint16_t value; /* 2 bytes */
char pad[6];
};
// private prototypes
FiloListUInt16Node *filo_list_uint16_node_new(uint16_t value);
void filo_list_uint16_node_free(FiloListUInt16Node *node);
//---------------------
// FiloListUInt16
//---------------------
FiloListUInt16 *filo_list_uint16_new(void) {
FiloListUInt16 *list = (FiloListUInt16 *)malloc(sizeof(FiloListUInt16));
list->first = NULL;
return list;
}
void filo_list_uint16_free(FiloListUInt16 *list) {
while (list->first != NULL) {
filo_list_uint16_pop(list, NULL);
}
free(list);
}
void filo_list_uint16_push(FiloListUInt16 *list, uint16_t value) {
FiloListUInt16Node *newNode = filo_list_uint16_node_new(value);
if (list->first != NULL)
newNode->next = list->first;
list->first = newNode;
}
bool filo_list_uint16_pop(FiloListUInt16 *list, uint16_t *i) {
if (list == NULL || list->first == NULL) {
return false;
}
FiloListUInt16Node *node = list->first;
if (i != NULL) {
*i = node->value;
}
list->first = node->next;
filo_list_uint16_node_free(node);
return true;
}
//---------------------
// FiloListUint16Node
//---------------------
FiloListUInt16Node *filo_list_uint16_node_new(uint16_t value) {
FiloListUInt16Node *node = (FiloListUInt16Node *)malloc(sizeof(FiloListUInt16Node));
node->next = NULL;
node->value = value;
return node;
}
void filo_list_uint16_node_free(FiloListUInt16Node *node) {
free(node);
}