EECS 280 Chapter 21: Structural Recurssion

61 views3 pages

Document Summary

The node structure of a linked list is self-similar, this means that we could have a list of 3 nodes, or a list of two of those nodes . Conceptually, a list is either: empty, a datum, followed by a sub-list. Recursive list size size(n) = 1 + size( the sub-list); The recursive list will continue until it hits the base case (until the next node has a null ptr). The base class is categorized by not doing any more code after it is called. Most often, it returns something and does nothing else. //recursive int list_size(node *n) { (adding 1) after you return size if(n==nullptr) return 0; return 1 + list_size(n->next); //it is still doing something. //tail recursive int list_size(node *n, int size) { if (n == nullptr) return size; size += 1; n = n->next; return list_size(n, size); The compiler can automatically know that it is tail recursive - so it can recycle the stack frame.

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
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions