Witam wszystkich mam problem z posortowaniem listy dwukierunkowej za pomocą sortowania bąbelkowego. Mój kod wygląda tak:
struct node {
int key;
struct node *left;
struct node *right;
};
void sort(int count, struct node *t_node)
{
struct node *tmp,*nextnode, *node1, *node2;
node2 = t_node;
do{
while(node2->right){
if (node2->key > node2->right->key) {
// zamiana B z C w liście A<->B<->C<->D na listę A<->C<->B<->D
// B = node2
nextnode = node2->right; // C
tmp = node2->left; // A
if (tmp)
tmp->right = nextnode; // A->right = C
if (nextnode->right)
nextnode->right->left = node2; //D->left = B
node2->right = nextnode->right; //B->right = D
node2->left = nextnode->left; //B->left = C
nextnode->right = node2; //C->right = B
nextnode->left = tmp; //C->left = A
}
node2 = node2->right;
}
count = count -1;
}while(count > 1);
}
Problem polega na tym, że dla przykładowych danych przed sortowaniem mam
Node value: 15
Node value: 3
Node value: 1
Node value: 10
A po sortowaniu:
Node value: 15
Node value: 1
Node value: 10
Gdzie mam błąd w kodzie proszę o pomoc.