有 3 个链接列表,其中 2 个已按顺序排列。2 个链表被排序到 3rd( headZ
) 中。如果我headZ
像它一样作为指针传递,它会在函数退出时恢复为空列表。如果我通过引用传递,它会退出并且headZ
只包含 1 个数字。我无法弄清楚如何让它工作。
void SortedRecur(Node*& headX, Node*& headY, Node* headZ){
if (headX == NULL && headY == NULL)
return;
else if (headX == NULL && headY != NULL)
{
if (headZ == 0)
{
headZ = headY;
headY = headY->link;
headZ->link = NULL;
}
else
{
headZ->link = headY;
headY = headY->link;
headZ = headZ->link;
headZ->link = NULL;
}
SortedRecur(headX, headY, headZ);
}
else if (headX != NULL && headY == NULL)
{
if (headZ == 0)
{
headZ = headX;
headX = headX->link;
headZ->link = NULL;
}
else
{
headZ->link = headX;
headX = headX->link;
headZ = headZ->link;
headZ->link = NULL;
}
SortedRecur(headX, headY, headZ);
}
if (headX != NULL && headY != NULL)
{
if (headX->data > headY->data)
{
if (headZ == NULL)
{
headZ = headY;
headY = headY->link;
headZ->link = NULL;
}
else
{
headZ->link = headY;
headY = headY->link;
headZ = headZ->link;
headZ->link = NULL;
}
}
else
{
if (headZ == NULL)
{
headZ = headX;
headX = headX->link;
headZ->link = NULL;
}
else
{
headZ->link = headX;
headX = headX->link;
headZ = headZ->link;
headZ->link = NULL;
}
}
SortedRecur(headX, headY, headZ);
}
cout << "ListZ: "; ShowAll(cout, headZ);} //Test contents of headZ