Anonymous user
Associative arrays/Creation/C: Difference between revisions
→From Scratch: include headers, use hash_t instead of Hash as struct name
(→From Scratch: remove hash_free function) |
(→From Scratch: include headers, use hash_t instead of Hash as struct name) |
||
Line 7:
A hash table can be implemented with the following. Because of this example's simplicity, it comes with some restrictions on use and capabilities: It can't be resized automatically, if you try to insert more values than its capacity it will freeze, the hashing function is very simple, etc. All are fixable with additional logic or using a library:
<lang c>
#include <stdlib.h>
typedef struct {
int size;
void **keys;
void **values;
}
return
}
int hash_index (
int
while (
return
}
void hash_insert (
int
}
void *hash_lookup (
int
return
}
int main () {
hash_insert(
hash_insert(
printf("hello => %s\n", hash_lookup(
printf("herp => %s\n", hash_lookup(
printf("a => %s\n", hash_lookup(
return 0;
}</lang>
|