-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhashmap.c
More file actions
44 lines (37 loc) · 795 Bytes
/
hashmap.c
File metadata and controls
44 lines (37 loc) · 795 Bytes
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
/* Hash map
* January 13, 2022 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char *key;
int value;
} item;
static item *linear_search(item *items, size_t size, const char *key) {
for (size_t i = 0; i < size; i++) {
if (strcmp(items[i].key, key) == 0) {
return &items[i];
}
}
return NULL;
}
int main(void) {
item items[] = {
{"one", 10},
{"two", 20},
{"three", 30},
{"four", 40},
{"five", 50}
};
size_t total = (sizeof(items) / sizeof(item));
char query[10];
printf("Enter a key to look up: ");
scanf("%s", query);
item *found = linear_search(items, total, query);
if (!found) {
fprintf(stderr, "String not found in linear search\n");
return 1;
}
printf("Value of \"%s\" is %d\n", query, found->value);
return 0;
}