-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
102 lines (87 loc) · 2.25 KB
/
main.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
** EPITECH PROJECT, 2018
** main.c
** File description:
** main
*/
#include "my.h"
size_t hash(char *name)
{
size_t result = 0;
for (size_t index = 0; name[index] != '\0'; index++) {
result += name[index] * (index + 1);
}
return (result);
}
hash_table_t *create_table(size_t size)
{
hash_table_t *new = malloc(sizeof(hash_table_t));
new->size = size;
new->table = malloc(sizeof(obj_t *) * size);
for (size_t index = 0; index != size; index++) {
new->table[index] = NULL;
}
return (new);
}
void destroy_table(hash_table_t *table)
{
for (size_t index = 0; index != table->size; index++) {
if (table->table[index] != NULL) {
free(table->table[index]);
}
}
free(table->table);
free(table);
}
obj_t *create_element(char *name, char *value)
{
obj_t *new = malloc(sizeof(obj_t));
new->name = name;
new->value = value;
new->next = NULL;
return (new);
}
void add_element(hash_table_t *table, char *name, char *value)
{
size_t index = hash(name) % table->size;
obj_t *tmp;
if (table->table[index] != NULL) {
for (tmp = table->table[index]; tmp->next != NULL && \
strcmp(tmp->name, name); tmp = tmp->next);
if (strcmp(tmp->name, name) == 0) {
tmp->value = value;
} else {
tmp->next = create_element(name, value);
}
} else {
table->table[index] = create_element(name, value);
}
}
char *get_element(hash_table_t *table, char *name)
{
size_t index = hash(name) % table->size;
obj_t *tmp;
if (table->table[index] != NULL) {
for (tmp = table->table[index]; tmp->next != NULL && \
strcmp(tmp->name, name); tmp = tmp->next);
if (strcmp(tmp->name, name) == 0) {
return (tmp->value);
} else {
return (NULL);
}
} else {
return (NULL);
}
}
int main(void)
{
hash_table_t *table = create_table(10);
char *name[] = {"KYT", "CAT", "TOTO", "TATA"};
char *value[] = {"A", "B", "C", "D"};
for (size_t index = 0; index != 4; index++) {
add_element(table, name[index], value[index]);
}
printf("[%s]\n", get_element(table, "TOTOO"));
destroy_table(table);
return (0);
}