
CS 331 Recursion and Linked Lists September 12, 2008
1 Objectives
Recursion is one of the most powerful ideas in Computer Science and in Mathematics. A proper understand-
ing of it is essential.
•Understand how recursion is related to induction
•Know three patterns of recursion:
Iterating, Mapping, Folding
•Be able to describe a list in terms of recursion.
•Know how to use nested classes to allow fast access to information about the list.
•Know how to implement a linked list in Java.
•Be able to draw a memory diagram for the operations of a linked list.
2 Examples
1public class List<E> {// use generics here.
2public class ListNode {
3E data;
4ListNode next; // add constructors, too.
5}
6
7ListNode first = null;
8ListNode last = null;
9ListNode cursor = null;
10 int size = 0;
11 }
3 Problems
Try the following problems. In a few minutes the instructor will go over the solutions. Feel free to work with
the person next to you!
•Write a method to return a list with every element incremented. (This is called functional style.)
•Write a method to increment every element of a list. (This is called imperative style, or destructive
update.)
•Write a method that returns the product of a list.
•Write the code to return the maximum element of a list.
•Which of the following operations will a last pointer speed up consistently?
–Delete the last element.
–Insert an element at the end of the list.
–Append two lists together.
Mattox Beckman Page 1 Illinois Institute of Technology