forked from paranlee/ludtm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashmap.c
More file actions
62 lines (48 loc) · 1.21 KB
/
Copy pathhashmap.c
File metadata and controls
62 lines (48 loc) · 1.21 KB
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
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <list.h>
#include <hashmap.h>
int main() {
// 1. Create hashmap (bucket size 16)
struct hash_map *map = hash_map_create(16);
if (!map) {
return 1;
}
printf("--- Inserting data ---\n");
hash_map_insert(map, "Alice", 25);
hash_map_insert(map, "Bob", 30);
hash_map_insert(map, "Charlie", 35);
hash_map_insert(map, "David", 40);
hash_map_insert(map, "Eve", 45);
hash_map_print(map);
// 2. Data update
printf("--- Updating 'Alice' to 26 ---\n");
hash_map_insert(map, "Alice", 26);
hash_map_print(map);
// 3. Data search
printf("--- Searching data ---\n");
int value;
if (hash_map_get(map, "Bob", &value)) {
printf("Found 'Bob': %d\n", value);
} else {
printf("'Bob' not found\n");
}
if (hash_map_get(map, "George", &value)) {
printf("Found 'George': %d\n", value);
} else {
printf("'George' not found\n");
}
// 4. Data deletion
printf("\n--- Deleting 'Charlie' ---\n");
hash_map_delete(map, "Charlie");
hash_map_print(map);
printf("--- Deleting 'Eve' ---\n");
hash_map_delete(map, "Eve");
hash_map_print(map);
// 5. Destroy hashmap
hash_map_destroy(map);
// The map pointer is now invalid.
return 0;
}