当我使用quicksort对双向链接列表进行排序时,用户会消失

时间:2019-05-28 14:10:10

标签: c++ sorting linked-list quicksort doubly-linked-list

我有一个双向链接列表,我想按用户的ELO对其排序。

问题在于,当调用函数swap()时,某些用户会消失

这是我在控制台中看到的: https://imgur.com/a/pJSPSnW

如您所见,第一次交换(玩家1和2之间)正确完成。 但是,当它交换玩家3和4时,user1消失了。

我认为问题出在函数swap()内,但我无法弄清楚确切位置

代码: 用户/节点声明

struct user {
public:
    string username;
    float ELO = NULL; //Score of the user       
    user* next = nullptr;
    user* previous = nullptr;
};

QuickSortRecursive函数

 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功能

void ELOManager::quickSort(){
    _quickSortRecursive(first, last);
}

分区功能

    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;
}

交换功能

    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();
}

随意进入我项目的github https://github.com/pablogalve/ELO-System

感谢您的帮助:)

2 个答案:

答案 0 :(得分:1)

在您的if (areTheyNeighbors(A, B))块中,链接列表指针最终看起来像这样:pointers 请注意:

  • B的两个指针都指向A
  • A和A->next都认为B是他们先前的列表元素

这会导致列表遍历中出现多个问题,并且可能是导致列表中第一个元素丢失的原因。

如果A和B是邻居,则应正确交换它们。

A->previous = B;
B->previous = swapperVector[0];
A->next = swapperVector[3];
B->next = A;

答案 1 :(得分:1)

Amanda的答案显示了一种处理相邻节点的方法,但如果先通过交换指向两个目标节点的内容来完成交换,则不需要特殊情况来处理相邻节点与不相邻节点,然后在两个目标节点内交换指针。

    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);