-
Notifications
You must be signed in to change notification settings - Fork 1
/
dictionary.c
77 lines (55 loc) · 1.26 KB
/
dictionary.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
#include <stdlib.h>
#include "dictionary.h"
dictionary* dictionary_new(void* key, void* value) {
dictionary_element *e = (dictionary*)malloc(sizeof(dictionary));
e->key = key;
e->value = value;
e->next = NULL;
return e;
}
void dictionary_add(dictionary* dictionary, void* key, void* value) {
dictionary_element *e, *last, *new;
if (dictionary == NULL)
return;
e = dictionary;
while (e != NULL) {
if (e->key == key) {
return;
}
e = e->next;
}
new = (dictionary_element*)malloc(sizeof(dictionary_element));
new->key = key;
new->value = value;
new->next = NULL;
e = last = dictionary;
while (e != NULL) {
last = e;
e = e->next;
}
last->next = new;
}
void* dictionary_get(dictionary* d, void* key) {
dictionary_element *e;
if (d == NULL)
return NULL;
e = d;
while (e != NULL) {
if (e->key == key) {
return e->value;
}
e = e->next;
}
return NULL;
}
void dictionary_destroy(dictionary* self) {
dictionary_element *e, *n;
if (self == NULL)
return;
e = self;
while (e != NULL) {
n = e->next;
free(e);
e = n;
}
}