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

4.) Rewrite the following recursive function to use iteration with an explicit s

ID: 3841201 • Letter: 4

Question

4.) Rewrite the following recursive function to use iteration with an explicit
stack.
int p r e o r d e r ( Tree t )
{
s t d : : cou t << t . item << s t d : : e o l ;
i f ( t . l e f t ) {
p r e o r d e r ( t . l e f t ) ;
}
i f ( t . r i g h t ) {
p r e o r d e r ( t . r i g h t ) ;
}
}


5.)Assume a linked list implementation identical to the 1st homework, or the
std::forward_list of the standard template library. Write a recursive function that
returns the sum of a linked list of integers given as its only argument.
int sum ( Lin k edLi s t <int> list_to_sum )
{
}

Explanation / Answer

4. void Preorder(Tree *root) { if (root == NULL) { return; } stack nodeStack; nodeStack.insert(root); while (nodeStack.empty() == false) { struct node *node = nodeStack.top(); printf ("%d ", node->data); nodeStack.delete(); if (node->right) { nodeStack.push(node->right); } if (node->left) { nodeStack.push(node->left); } } } 5. int sum(linkedlist list_to_sum) { if(node != NULL) { node = node->data; list_to_sum=list_to_sum+node; return sum(node->next,list_to_sum); } else { return list_to_sum; } }
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