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);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.