-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashtable_simple.c
131 lines (101 loc) · 2.98 KB
/
hashtable_simple.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define MAX_NAME 256
#define TABLE_SIZE 10
#define DELETED_NODE ((person*) (0xFFFFFFFFFFFFFFFFUL))
typedef struct {
char name[MAX_NAME];
int age;
} person;
person* hash_table[TABLE_SIZE];
unsigned int hash(const char* name) {
unsigned int hash_value = 5381; // Initial value for DJB2
int c;
while ((c = *name++)) {
hash_value = ((hash_value << 5) + hash_value) + c; // DJB2 Hash Function
}
return hash_value % TABLE_SIZE;
}
void init_hash_table() {
for (int i = 0; i < TABLE_SIZE; i++) {
hash_table[i] = NULL;
}
}
void print_table() {
printf("START\n");
for (int i = 0; i < TABLE_SIZE; i++) {
if (hash_table[i] == NULL) {
printf("\t%i\t---\n", i);
} else if (hash_table[i] == DELETED_NODE) {
printf("\t%i\t---<DELETED>\n", i);
} else {
printf("\t%i\t%s\n", i, hash_table[i]->name);
}
}
printf("END\n");
}
bool hash_table_insert(person* p) {
if (p == NULL) return false;
unsigned int index = hash(p->name);
for (int i = 0; i < TABLE_SIZE; i++) {
unsigned int try = (index + i) % TABLE_SIZE;
if (hash_table[try] == NULL || hash_table[try] == DELETED_NODE) {
hash_table[try] = p;
return true;
}
}
return false;
}
person* hash_table_delete(const char* name) {
unsigned int index = hash(name);
for (int i = 0; i < TABLE_SIZE; i++) {
unsigned int try = (index + i) % TABLE_SIZE;
if (hash_table[try] == NULL) return NULL;
if (hash_table[try] == DELETED_NODE) continue;
if (strcmp(hash_table[try]->name, name) == 0) {
person* tmp = hash_table[try];
hash_table[try] = DELETED_NODE;
return tmp;
}
}
return NULL;
}
person* hash_table_lookup(const char* name) {
unsigned int index = hash(name);
for (int i = 0; i < TABLE_SIZE; i++) {
unsigned int try = (index + i) % TABLE_SIZE;
if (hash_table[try] == NULL) return NULL;
if (hash_table[try] == DELETED_NODE) continue;
if (strcmp(hash_table[try]->name, name) == 0) return hash_table[try];
}
return NULL;
}
int main() {
init_hash_table();
print_table();
person lucas = {.name = "Lucas", .age = 256};
person luisa = {.name = "Luisa", .age = 18};
person nazira = {.name = "Nazira", .age = 32};
person ron = {.name = "Ron", .age = 4};
hash_table_insert(&lucas);
hash_table_insert(&luisa);
hash_table_insert(&nazira);
hash_table_insert(&ron);
print_table();
person* tmp = hash_table_lookup("Lucas");
if (tmp == NULL) {
printf("NOT FOUND\n");
} else {
printf("FOUND %s.\n", tmp->name);
}
tmp = hash_table_lookup("Nadir");
if (tmp == NULL) {
printf("NOT FOUND\n");
} else {
printf("FOUND %s.\n", tmp->name);
}
hash_table_delete("Luisa");
print_table();
return 0;
}