For practice, I’ve been working on a compressor which does the find-repeated-parts, make-dictionary, compress-with-huffman codes thing.
It’s not really working.
One of the problems is, for some reason my sorting algorithm drops keywords from the dictionary. I think the problem is in the swap routine, but I’m not sure.
( this routine swaps adjacent keywords, with next being current->next ).
I do have a static keyword * head;
void swap(keyword * current, keyword * next) {
keyword * prev = current->prev;
if (prev){
prev->next = next;
next->prev = prev;
} else { /* no prev - current is head */
head = next;
next->prev = 0;
}
current->prev = next;
current->next = next->next;
next->next = current;
}
Spot anything wrong with this?
You don’t set
next->next->prev.Getting data-structure implementations correct is notoriously difficult. The way to spot this sort of thing is to work out how many pointers should need updating (6). You’re only updating 5, so one must be missing!