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

Linked List struct LinkedNode { int data; LinkedNode * next; }; Write the code f

ID: 3758157 • Letter: L

Question

Linked List

struct LinkedNode

{

int data;

LinkedNode * next; };

Write the code for each of the two functions given below:

int countPositiveIterative(const LinkedNode * start)

{

//return a count on how many 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 countPositiveRecursion(const LinkedNode * start)

{

//return a count on how many data values in the linked list are positive

//must be a recursive function, no explicit while or for loops are allowed

//start holds the address of the first node on the linked list

//if the linked list has no members, start holds the value of nullptr

}

Explanation / Answer

int countPositiveIterative(const LinkedNode * start)

{

if(start==NULL)

return 0;

else

{

int count =0 ;

  

LinkedNode *temp = (LinkedNode*)start;

  

while (temp) {

if(temp->data>=0)

count++;

  

temp = temp->next;

}

return count;

}

}

int countPositiveRecursion(const LinkedNode * start)

{

static int count = 0;

if(start == NULL)

return count;

else

{

if (start->data>=0) {

count = count+1;

}

return countPositiveIterative(start->next);

}

}

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