Hello,
I now simply set the hash value to the same as the data value and can change that later. The array does not contain duplicates.
When I run the debugger, I get this:
Code:
Breakpoint 1, insert_into (table=0x7fffffffdd70, data=3) at myHashmap.c:16warning: Source file is more recent than executable.
16 Entry* entry = (Entry*)malloc(sizeof(Entry));
(gdb) n
17 entry->hash = data;
(gdb) n
18 entry->value = data;
(gdb) n
19 table[entry->hash] = entry;
(gdb) p entry->hash
$1 = 3
(gdb) p table[entry->hash]
$2 = (Entry *) 0x1
(gdb) n
20 entry->next = NULL;
(gdb) p table[entry->hash]
$3 = (Entry *) 0x5555555592a0
I wonder how the value 0x1 gets determined. This is my code:
Code:
#include <stdio.h>#include <stdlib.h>
#define TABLE_SIZE 16
typedef int Data;
typedef struct entry Entry;
struct entry {
Data value;
unsigned int hash;
Entry* next;
};
void insert_into(Entry* table[], Data data){
Entry* entry = (Entry*)malloc(sizeof(Entry));
entry->hash = data;
entry->value = data;
table[entry->hash] = entry;
entry->next = NULL;
}
int main(){
/**
Entry* test = (Entry*) malloc(sizeof(Entry));
printf("%p\n", test);
test->hash = (unsigned int) test;
printf("%d\n", test->value);
**/
Data data_points[5] = {3,5,2,12,9};
Entry* lookup_table[TABLE_SIZE];
for (int i = 0; i < 5; i++){
insert_into(lookup_table, data_points[i]);
}
for (int i = 0; i < TABLE_SIZE; i++)
printf("%d = %d\n", i, lookup_table[i]->value);
return 0;
}