-
Notifications
You must be signed in to change notification settings - Fork 0
/
name.c
79 lines (72 loc) · 1.61 KB
/
name.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
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
// Name node
typedef struct _name {
int value, time_stamp;
struct _name *next;
} Name;
// Name constructor
// if value = -1 new name
// if value = -2 new name is 0 mod 3
// if value = -3 new name is 1 mod 3
// if value = -4 new name is 2 mod 3
Name *create_name(int value, int time_stamp) {
static int new = 0;
static int new_0 = 0;
static int new_1 = 1;
static int new_2 = 2;
switch (value) {
case -1:
value = ++new;
break;
case -2:
value = new_0;
new_0 += 3;
break;
case -3:
value = new_1;
new_1 += 3;
break;
case -4:
value = new_2;
new_2 += 3;
break;
}
Name *name = malloc(sizeof(Name));
assert(name != NULL);
*name = (Name){value, time_stamp, NULL};
return name;
}
// Check if value is in list
bool contain_name(Name *name, int value, int time) {
while (name != NULL) {
if (name->value == value && (time == -1 || name->time_stamp >= time)) {
return true;
}
else {
name = name->next;
}
}
return false;
}
// Clone a name list (set tail to last name cloned)
Name *clone_name(Name *src, Name **tail) {
if (src == NULL) {
*tail = NULL;
return NULL;
}
Name *name = create_name(src->value, src->time_stamp);
name->next = clone_name(src->next, tail);
if(name->next == NULL) {
*tail = name;
}
return name;
}
// Deconstructor
void delete_name(Name *name) {
if (name != NULL) {
delete_name(name->next);
free(name);
}
}