移动下一个节点返回功能

时间:2013-10-09 02:28:21

标签: c++ pointers dll

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

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;

}

我缺少什么?

1 个答案:

答案 0 :(得分:1)

这应该有效

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