C++ I have a small question in this piece of code. I wanna add that if there are
ID: 3866859 • Letter: C
Question
C++ I have a small question in this piece of code. I wanna add that if there are no nodes return -1; and then in main i can call the "display error" Please if you do not know don't answer it. and no it is not as simple as:
if (node == NULL)
{
return -1;
} //This would be wrong and totally wrong so please chegg answerers don't answer if you do not know
int IntList::numNodes(ListNode* node)
{
int count = 1;
if (node == NULL)
{
return 0;
}
else
{
count += numNodes(node->next);
return count;
}
}
Explanation / Answer
int IntList::numNodes(ListNode* node)
{
int count = 1;
if (node == NULL) throw memoryAllocation("node allocation error");
{
return -1 ;//return the address of the node on the heap
}
else
{
count += numNodes(node->next);
return count;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.