Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ make these functions validate with return -1 if the list is empty so that is

ID: 3866864 • Letter: C

Question

C++ make these functions validate with return -1 if the list is empty so that is they are called in main they will return the appropiate message ex.if (position == -1)
    {
    cout << "This ain't working ." << endl;
    }
    else
    {
    cout << "here you go sweety: " << myList.numNodes(myList.head) << endl;
    }

int NumberList::numNodes(ListNode* node)
{
    int count = 1; //to start count at number 1 and not zero /initilize count

    if (node == NULL) //while there are nodes
    {
    return 0;
    }
    else
    {
    count += numNodes(node->next);
    return count;
    }
}

//largest value

int NumberList::maxNode(ListNode* node, int largest)
{
    if (node == NULL)
    {
    return largest;
    }
    if (node->value > largest)
    largest = node->value;
    return maxNode(node->next, largest);
}

Explanation / Answer

int NumberList::numNodes(ListNode* node)
{
if (node == NULL) //while there are no nodes

    {
    return 0; //When 0 is returned it means no nodes are available
    }
    else
    {
    return 1+numNodes(node->next); // We are incrementing the node values
  
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote