I’m having trouble to implement a simple list in C, the problem is the connection of the items via pointers.
The following piece of code is a snippet from a hashtable, which is supposed to store items with the same index in a list to avoid collisions.
typedef struct dictEntry {
void *key;
void *value;
struct dictEntry *next;
} dictEntry;
typedef struct dict {
dictEntry **table;
unsigned long size;
unsigned long used;
} dict;
void dictAdd(dict *d, void *key, void *value) {
int index = hash(key) & d->size;
dictEntry *entry;
entry = malloc(sizeof(entry));
entry->key = key;
entry->value = value;
entry->next = 0;
if (d->table[index]) {
/* this is does not work */
dictEntry *next;
next = d->table[index];
while (next) {
next = next->next;
}
next = entry;
} else {
d->table[index] = entry;
d->used++;
}
}
My thinking was to iterate through every element of the list (next->next) and assign the pointer of entry to the last element (next = entry;).
After a few days of rewriting and moving parts of the code around, I still can’t seem to find a solution.
You should try to implement the linked list first.
Here’s how I would implement the addition to the end (I’ve modified your code where you just overwrite the temporary “next” variable without modifying the list itself):
….