gpt4 book ai didi

c++ - 向后移动下一个节点函数

转载 作者:行者123 更新时间:2023-11-28 07:22:09 24 4
gpt4 key购买 nike

所以我正在尝试为名为 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;

}

有什么我想念的吗?

最佳答案

这应该可行

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

关于c++ - 向后移动下一个节点函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19262011/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com