## CSE 131 Module 9: Lists

### Exercises

These exercises are part of the asynchronous component of this course. Material is taught and demonstrated, but at times you are asked to complete one of the exercises to help you master the new concepts.

In the repository you are using for this course, look inside the exercises source folder, and you will see assignments organized by name. Before continuing on to the next portion of the asynchronous material, spend some time on the exercise. When you are finished, commit your code so that credit for the exercise can be given.

It is not so important that you get the exercise exactly right. It is important to work through it as best you can before you look at the solution or continue with other work in this module.

Before starting this exercise:
Be sure to update your repository before beginning this assignment, so that you receive any code you need.

### A static and interative size(ListNode<T>) method

1. In the exercises9 package of your exercises folder, open the ListNode class.
2. You will find there a stub method for size(ListNode<T>).
• A stub is the starting sketch of a method. It usually returns some default result, which, while not correct, is a starting point for developing a real implementation of the method.
• Because this method is static, it does not have access to the class parameter type T. Thus you see that the method declaration:
```public static<T> int size(ListNode<T> start)
```
declares the type parameter T on the static attribute.
3. Complete the method so that it computes the size of a sequence of ListNode<T> objects, where the list is implied to begin at the parameter start.
You must use iteration, not recursion, for your solution. You can review the video with the students in which they worked on a recursive solution. But your solution here must be iterative.
4. To test your code, uncomment the lines of code in the main method that call the size(ListNode<T>) method.
If you get stuck, take a look at the solution video but look only as far as you must to get unstuck. At that point, resume work on your own.
After completing this exercise:
Commit any files you have changed so that you receive credit for your work!

Before starting this exercise:
Be sure to update your repository before beginning this assignment, so that you receive any code you need.

### A static and recursive size(ListNode<T>) method

In this exercise, you continue your work from the previous exercise.
1. In the exercises9 package of your exercises folder, open the ListNode class, which should contain your work from the previous exercise.
2. Rename the existing size(ListNode<T>) method to some other name (such as sizeIterative, and define a new size(ListNode<T>) method for your work here.
3. Complete the method so that it computes the size of a sequence of ListNode objects, where the list is implied to begin at the parameter start.
• Here you must use recursion for your solution.
• The interactive student video does exactly that, so you can review it if necessary. However, try to do this on your own as much as possible.
• The solution for this exercise contains another video showing how to solve this problem.
4. To test your code, use the lines of code in the main method that call the size(ListNode<T>) method.
If you get stuck, take a look at the solution videos but look only as far as you must to get unstuck. At that point, resume work on your own.
After completing this exercise:
Commit any files you have changed so that you receive credit for your work!

Before starting this exercise:
Be sure to update your repository before beginning this assignment, so that you receive any code you need.

### A static and recursive sum(ListNode<T>) method

In this exercise, you continue your work from the previous exercise.
1. In the exercises9 package of your exercises folder, open the ListNode class, which should contain your work from the previous exercises.
2. Define a method stub by inserting the following code into your class:
```	//
// static and iterative
//
public static<T> int sum(ListNode<T> start) {
// NB  start could be null!
return 0; // FIXME
}
```
3. Complete the method so that it computes the sum of a sequence of ListNode objects's values, where the list is implied to begin at the parameter start.
4. To test your code, use the lines of code in the main method that call the sum(ListNode<T>) method.
If you get stuck, take a look at the solution videos but look only as far as you must to get unstuck. At that point, resume work on your own.
After completing this exercise:
Commit any files you have changed so that you receive credit for your work!
```

```
```

```
```

```
```

```