I'm trying to swap two pointers of a singly linked list using the bubble sort.
I've made the compare function, and its working good.
In the swap function, the swap is working good, I've managed to swap between the node and the node->next
, though the linked list "lose" the info of the node (after the swap), so the first node in the linked list is node->next
.
I'm using a generic function which do the bubble sort and call the compare function and the swap function.
Any idea why this happens?
void swap_arr(void **arr,int i , int j)
{
Team *teamList = (Team*) arr ;
Team *teamI = (Team*) arr , *teamJ ;
Team *temp ;
Team *temp1;
int z;
// Receives instead i
for(z=0; z<i; z++)
teamI = teamI->next;
//teamJ is the i+1
teamJ = teamI->next;
temp = teamI;
temp1 = teamJ->next;
teamI = teamJ ;
teamJ = temp;
if (temp1->next->next==NULL)
teamJ->next = NULL;
else
teamJ->next = temp1->next;
teamI->next = teamJ;
if (temp1==NULL)
teamJ->next=NULL;
else
teamJ->next = temp1;
}