有3個鏈表,其中2個(headX和headY)按降序排列,函數作業是將它們排序成單個列表(headZ)。C指針在遞歸中恢復到舊地址
由於某些原因,該算法可以正常工作,但是當它完成並通過遞歸返回時,3個指針在通過遞歸「返回」時恢復爲舊值。
void SortedMergeRecur(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;
}
SortedMergeRecur(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;
}
SortedMergeRecur(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;
}
}
SortedMergeRecur(headX, headY, headZ);
}
}
我知道,這部分可以簡化(如追加整個列表如果任headX或令人興奮的是空的),但我寫的這種方式,因爲我認爲它可能會解決問題,它沒有....
你期望什麼?這聽起來像你想通過引用傳遞指針。 – oldrinb