将下一个节点移回功能

Move Next Node Back Function

本文关键字:功能 节点 下一个      更新时间:2023-10-16

所以我正在尝试为DLL类编写一个名为MoveNextToBack的函数。该函数的目的是将节点的下一个节点移动到列表的后面。这是我到目前为止所拥有的,但我认为它不完整:

void DLL::MoveNextToBack(Node *N){
    // If N's next node is the end already, return
    if(N->Next == Tail)
        return;
    // Change N's Next pointer to the one after N's current Next
    N->Next = N->Next->Next;
    // Change N's Next Next's Previous pointer to point to N
    N->Next->Next->Prev = N;
    // Move N to the end
    N->Next->Next = Tail;
    N->Next->Prev = Tail->Prev;
}

我错过了什么?

这应该有效

void DLL::MoveNextToBack(Node *N){
    // If N's next node is the end already, return
    if(N == NULL || N->Next == NULL || N->Next == Tail)
        return;
    //Pointer to the next node
    Node *tmp = N->Next
    //Point to the next, next node
    Node *pmt = N->Next->Next;
    // Change N's Next pointer to the one after N's current Next
    N->Next = pmt;
    // Change N's Next Next's Previous pointer to point to N
    if (pmt != NULL)
        pmt->Prev = N;
    // Move tmp to the end
    tmp->Prev = Tail;
    tmp->Next = Tail->Next;
    Tail->Next = tmp;
    Tail = tmp;
}