Hi all,
I'm just asking for help for my linked list sort function, I'm not including my entire program, as I'm sure my problem lies in my sort.
I need to sort the linked list by rearranging the pointers, I was fairly sure I got the function correct. When I test the sort function it isn't sorting properly. I can't see what exactly is wrong. If anyone can find anything I'd greatly appreciate it!
The list is to be in ascending order by 'frequency'.
//////////
struct huffman_data
{
char letter;
int frequency;
//huffman_data *left;
//huffman_data *right;
huffman_data *linker;
};
/////////////
void sort_linker_list(huffman_data *head)
{
huffman_data *temp, *temp1, *temp2;
for(temp1 = head; temp1 != NULL; temp1 = temp1->linker)
{
for(temp2 = temp1->linker; temp2 != NULL; temp2 = temp2->linker)
{
if(temp1->frequency > temp2->frequency)
{
if(temp1 != head)
{
temp = head;
while(temp->linker != temp1)
{
temp = temp->linker;
}
temp1->linker = temp2->linker;
temp2->linker = temp->linker;
temp->linker = temp2;
}
else
{
head->linker = temp2->linker;
temp2->linker = head;
head = temp2;
}
}
}
}
for(huffman_data *temp = head; temp != NULL; temp = temp->linker)
cout << temp->letter << " " << temp->frequency << endl;
cout << endl;
return;
}