CP164 Final: auxiliary function

36 views2 pages
14 Jun 2018
School
Course
Professor
Linked Recursion
The solution is to use an auxiliary function that works with the node portion of the List only.
Although we can claim that print_r is recursive, the actual recursion is done in its auxiliary
function. This is print_r itself:
def print_r(self):
"""
-------------------------------------------------------
Prints the contents of list from rear to front.
-------------------------------------------------------
Postconditions:
Prints each value in list.
-------------------------------------------------------
"""
self._print_r_aux(self._front)
return
Note that print_r has no extra nodes and does not recursively call itself. Instead, it calls a
private auxiliary function. This function is passed the front of the node list rather than the entire
List.
This auxiliary function works directly with the node data:
def _print_r_aux(self, current):
"""
-------------------------------------------------------
Prints the contents of the current node.
-------------------------------------------------------
Postconditions:
Prints the value in the current node.
-------------------------------------------------------
"""
if current is not None:
self._print_r_aux(current._next)
print(current._data)
return
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in

Document Summary

The solution is to use an auxiliary function that works with the node portion of the list only. Although we can claim that print_r is recursive, the actual recursion is done in its auxiliary function. Prints the contents of list from rear to front. Note that print_r has no extra nodes and does not recursively call itself. This function is passed the front of the node list rather than the entire. This auxiliary function works directly with the node data: def _print_r_aux(self, current): Prints the value in the current node. if current is not none: self. _print_r_aux(current. _next) print(current. _data) return. Note that auxiliary recursive functions generally have more parameters and/or different parameters than the base function that calls them. If you write an auxiliary function that uses the same number and type of parameters as the base function that calls it, then you probably don"t need the auxiliary function (or you wrote it incorrectly).

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers

Related Documents