-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.c
49 lines (33 loc) · 826 Bytes
/
lib.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
// define a node
typedef struct Node {
char* id;
void* data;
struct Node* next;
} Node;
// gen id
char* random_id(int32_t length) {
char* dest = (char*) malloc(sizeof(char) * (length + 1));
char charset[] = "0123456789"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int32_t i = 0;
while (i < length) {
int32_t index = (double) rand() / RAND_MAX * (sizeof charset - 1);
dest[i] = charset[index];
i++;
}
dest[length] = '\0';
return dest;
}
// setup a node
Node* new_node(void* data) {
Node* n = (Node *) malloc(sizeof(Node));
n->id = random_id(8);
n->data = data;
n->next = NULL;
return n;
}