struct LinkedList { int data; LinkedNode * next; }; //write the code for each of
ID: 3757917 • Letter: S
Question
struct LinkedList
{
int data;
LinkedNode * next;
};
//write the code for each of the two functions give below:
int countPositiveIterative(const LinkedNode * start)
{
//return a count on how maniy data values in the linked list are positive
// a loop is needed in this function, recursion cannot be used
// start holds the address of the first node in the linked list
// if the linked list has no members, start hold the value of nullptr
}
int countPositiveRecurssion( const LinkedNode * start)
{
//return a count on how many data value in the linked list are positive
//must be recurrsive function, no explicit while or for loops are allowed
//start holds the address of the first node in the linked list
// if the list has no members, start holds the value of nullptr
}
no need to write the driver just these two methods
Explanation / Answer
struct LinkedNode
{
int data;
LinkedNode * next;
};
//write the code for each of the two functions give below:
int countPositiveIterative(const LinkedNode * start)
{
//return a count on how maniy data values in the linked list are positive
// a loop is needed in this function, recursion cannot be used
// start holds the address of the first node in the linked list
// if the linked list has no members, start hold the value of nullptr
int count = 0;
while(start != nullptr) {
if(start->data > 0)
count++;
start = start -> next;
}
return count;
}
int countPositiveRecurssion( const LinkedNode * start)
{
//return a count on how many data value in the linked list are positive
//must be recurrsive function, no explicit while or for loops are allowed
//start holds the address of the first node in the linked list
// if the list has no members, start holds the value of nullptr
if (start == nullptr) {
return 0;
}
if (start->data > 0) {
return 1 + countPositiveRecurssion(start->next);
} else {
return 0 + countPositiveRecurssion(start->next);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.