以下程式:降冪多項式的鏈表形式相加函式,在運行到下方紅色字部分時顯示讀取資料錯誤,除錯發現此時的資料為p1=???,這時應該觸發外層while回圈中的條件而跳出,但是總是跳不出,求助大神為什么
poly* add(poly* p1, poly* p2)
{
poly* result = nullptr;
poly* result_head = nullptr;
while (p2!=nullptr&&p1!=nullptr)
{
if (p1->times < p2->times)
{
if (result != nullptr)
result=result->next = p2;
else result = p2;
p2 = p2->next;
if (result_head == nullptr)result_head = result;
}
if (p1->times > p2->times)
{
if (result != nullptr)
result=result->next = p1;
else result = p1;
p1 = p1->next;
if (result_head == nullptr)result_head = result;
}
if (p1->times == p2->times)
{
poly* temp = (poly*)malloc(sizeof(poly));
temp->coef = p1->coef + p2->coef;
temp->times = p1->times;
if (temp->coef!=0)
{
p1 = p1->next;
p2 = p2->next;
if (result != nullptr)
result=result->next = temp;
else result = temp;
if (result_head == nullptr)result_head = result;
}
else//如果系數和為0
{
p1 = p1->next;
p2 = p2->next;
}
}
}
if (p1 == nullptr&&p2!=nullptr) result=result->next = p2;
if (p1 != nullptr && p2 == nullptr)result=result->next = p1;
if (p1 == nullptr && p2 == nullptr);
return result_head;
}

測驗資料:
4
3 4 -5 2 6 1 -2 0
3
5 20 -7 4 3 1
(本意是實作降冪多項式的鏈表形式相加)
uj5u.com熱心網友回復:
p2沒有賦值uj5u.com熱心網友回復:
你好,這里確實顯示的是p2沒有值,但按理來說這時應該已經跳出最外層的while回圈了,為什么他還會在里面呢?轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/27666.html
標籤:C++ 語言
