Compare commits
No commits in common. "13932df1a87ac331a919fafcb5fbd955e161cb28" and "2a38f98d37073a15d23d4d5bec0d62f811df186b" have entirely different histories.
13932df1a8
...
2a38f98d37
|
@ -1,37 +0,0 @@
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
typedef struct item {
|
|
||||||
char *key;
|
|
||||||
int value;
|
|
||||||
} item;
|
|
||||||
|
|
||||||
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() {
|
|
||||||
item items[] = {
|
|
||||||
{"foo", 10},
|
|
||||||
{"bar", 20},
|
|
||||||
{"hello", 30},
|
|
||||||
{"x", 40}};
|
|
||||||
|
|
||||||
size_t total_items = sizeof(items) / sizeof(items);
|
|
||||||
|
|
||||||
item *found = linear_search(items, total_items, "foo");
|
|
||||||
|
|
||||||
if (found == NULL) {
|
|
||||||
printf("linear search: value of 'foo' has no key\n");
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("linear search: value of 'foo' is: %d", found->value);
|
|
||||||
return (0);
|
|
||||||
}
|
|
|
@ -1,69 +0,0 @@
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
typedef struct HashTable {
|
|
||||||
char *data[16];
|
|
||||||
} HashTable;
|
|
||||||
|
|
||||||
HashTable *new_hashtable() {
|
|
||||||
HashTable *ht = malloc(sizeof(HashTable));
|
|
||||||
for (int i = 0; i < 16; i++) {
|
|
||||||
ht->data[i] = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (ht);
|
|
||||||
}
|
|
||||||
|
|
||||||
int get_index_for_letter(char letter) {
|
|
||||||
if (letter >= 'a' && letter <= 'z') {
|
|
||||||
return (letter - 'a') + 1;
|
|
||||||
} else {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int hash(char *word) {
|
|
||||||
int word_len = strlen(word);
|
|
||||||
int accum = 1;
|
|
||||||
int index;
|
|
||||||
for (int i = 0; i < word_len; i++) {
|
|
||||||
index = get_index_for_letter(word[i]);
|
|
||||||
accum *= index;
|
|
||||||
}
|
|
||||||
|
|
||||||
return accum % 16;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *get(HashTable *h, char *key) {
|
|
||||||
int hash_val = hash(key);
|
|
||||||
return h->data[hash_val];
|
|
||||||
}
|
|
||||||
|
|
||||||
void insert(HashTable *h, char *key, char *value) {
|
|
||||||
int hashed_key = hash(key);
|
|
||||||
|
|
||||||
if (h->data[hashed_key] != NULL) {
|
|
||||||
fprintf(stderr, "ERROR: collision!");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
h->data[hashed_key] = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
void delete(HashTable *h, char *key) {
|
|
||||||
int hashed_key = hash(key);
|
|
||||||
h->data[hashed_key] = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
HashTable *hash_table = new_hashtable();
|
|
||||||
char *key = "hello";
|
|
||||||
insert(hash_table, key, "world");
|
|
||||||
insert(hash_table, "hlleo", "world");
|
|
||||||
char *res = get(hash_table, "hello");
|
|
||||||
|
|
||||||
printf("the value at key '%s' is '%s'", key, res);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
Loading…
Reference in New Issue