template<typename T>
void doubleCircularList<T>::merge(doubleCircularList<T> & a, doubleCircularList<T> & b)
{
if (!a.firstNode || !b.firstNode)
{
if (!a.firstNode && !b.firstNode)
return;
else if (a.firstNode)
{
firstNode = a.firstNode;
lastNode = a.lastNode;
}
else
{
firstNode = b.firstNode;
lastNode = b.lastNode;
}
}
else
{
ListNode<T>*p = a.firstNode;
ListNode<T>*q = b.firstNode;
ListNode<T>*currentNode = nullptr;
a.lastNode->next = b.lastNode->next = nullptr;
while (p && q)
{
if (p->element <= q->element)
{
if (currentNode)
{
currentNode->next = p;
currentNode->next->previous = currentNode;
currentNode = currentNode->next;
p = p->next;
}
else
{
currentNode = firstNode = p;
p = p->next;
}
}
if (p && p->element > q->element)
{
if (currentNode)
{
currentNode->next = q;
currentNode->next->previous = currentNode;
currentNode = currentNode->next;
q = q->next;
}
else
{
currentNode = firstNode = q;
q = q->next;
}
}
}
if (p)
{
currentNode->next = p;
currentNode->next->previous = currentNode;
lastNode = a.lastNode;
firstNode->previous = lastNode;
lastNode->next = firstNode;
}
if (q)
{
currentNode->next = q;
currentNode->next->previous = currentNode;
lastNode = b.lastNode;
firstNode->previous = lastNode;
lastNode->next = firstNode;
}
}
ListSize = a.ListSize + b.ListSize;
a.firstNode = a.lastNode = b.firstNode = b.lastNode = nullptr;
a.ListSize = b.ListSize = 0;
}
哪个大佬,写一份不令尾节点置空的方法,我这个是令尾节点置空的方法,感觉不好
我自己写好了