I have a doubly-linked list and I want to sort the users by their ELO.
The problem is that when the function swap() is called, some users dissapear
This is what I get in my console: https://imgur.com/a/pJSPSnW
As you can see, the first swap (between players 1 and 2) is done correctly. But, when it swaps players 3 and 4, user1 dissapears.
I think that the problem is inside the function swap(), but I can't figure out where exactly
Code: User/Node declaration
struct user {
public:
string username;
float ELO = NULL; //Score of the user
user* next = nullptr;
user* previous = nullptr;
};
QuickSortRecursive function
void ELOManager::_quickSortRecursive(user* low, user* high) {
if (high != nullptr && low != high && low != nullptr) {
user* p = partition(low, high);
_quickSortRecursive(low, p->previous);
_quickSortRecursive(p->next, high);
}
}
QuickSort function
void ELOManager::quickSort(){
_quickSortRecursive(first, last);
}
Partition function
user* ELOManager::partition(user* low, user* high) {
float pivot = high->ELO;
user* i = low->previous;
for (user* j = low; j != high && j!=nullptr; j = j->next) {
if (j->ELO <= pivot){
i = (i == NULL) ? low : i->next;
swap(i, j);
}
}
//i = (i == NULL) ? low : i->next;
swap(i->next, high);
cout << endl << "Loop finished -----------------------" << endl;
printUsers();
return i;
}
Swap function
void ELOManager::swap(user* A, user* B) {
user* tmp = new user();
user* swapperVector[4];
cout << endl << "swap1[" << A->username << "]" << endl;
cout << "swap2[" << B->username << "]" << endl;
if (A == B) {
cout << "Same Users: Continue" << endl;
return;
}
swapperVector[0] = A->previous;
swapperVector[1] = B->previous;
swapperVector[2] = A->next;
swapperVector[3] = B->next;
if (areTheyNeighbours(A, B)) {
A->previous->next = B;
A->next->previous = B;
B->previous->next = A;
B->next->previous = A;
A->previous = B;
B->previous = swapperVector[1];
A->next = swapperVector[3];
B->next = A;
cout << endl << "Option 1" << endl;
}
else {
A->previous = swapperVector[1];
B->previous = swapperVector[0];
A->next = swapperVector[3];
B->next = swapperVector[2];
A->previous->next = B;
A->next->previous = B;
B->previous->next = A;
B->next->previous = A;
cout << endl << "Option 2" << endl;
}
cout <<"Print list after swap" << endl << "-----" << endl;
printUsers();
}
Feel free to get into my project's github https://github.com/pablogalve/ELO-System
I would appreciate your help :)
In your if (areTheyNeighbors(A, B))
block, your linked list pointers end up looking like this: Notice that:
A->next
think B is their previous list element This causes multiple problems in list traversal, and might be what causes the first element in the list to be lost.
If A and B are neighbors, this should swap them properly.
A->previous = B;
B->previous = swapperVector[0];
A->next = swapperVector[3];
B->next = A;
Amanda's answer shows one way to deal with adjacent nodes, but there doesn't need to be a special case to handle adjacent versus non-adjacent nodes, if the swapping is done first by swapping what is pointing to the two target nodes, then swapping the pointers within the two target nodes.
AP = A->previous;
BP = B->previous;
AN = A->next;
BN = B->next;
std::swap(AN->previous, BN->previous);
std::swap(AP->next, BP->next);
std::swap(A->previous, B->previous);
std::swap(A->next, B->next);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.