---
# System prepended metadata

title: Lecture 10 Setup/Prep
tags: [Setup-Summer21]

---

---
tags: Setup-Summer21
---

# Lecture 10 Setup/Prep

We have now been through three implementations of lists as sequences of nodes:

1. A straight-up sequence of nodes, ending in `EmptyList`
2. A list object that contains a sequence of nodes ending in `EmptyList`
3. A list object that contains a sequence of nodes ending in `null`

We have seen that there are differences between these in whether the list contents change when adding elements. We've also seen that using `null` to end a list saves on the space of an `EmptyList` object, at the cost of having methods always checking whether it is at the end of the list.

Separately, we've looked at using variables to improve the running time of some methods on lists: we were able to reduce `addLast` from linear time to constant time by storing the `end` parameter in the `LinkList`. A similar optimization saves time for computing the length of the list.

What about methods such as `get`, which fetch elements by position? Can we optimize those? To think about that, we have to take a closer look at precisely *where* objects get placed in memory.

## Prep

**There is no starter code for this lecture. Just set up a new package for this lecture. We'll write the code from scratch.**

We will start class looking at [this Google Sheet](https://docs.google.com/spreadsheets/d/1GEn5MaRnwy3kt1R1cxl1XOUbRWYf_Yd-JfpLh9ywWlQ/edit?usp=sharing) which shows how list objects are laid out in memory in more detail. Specifically:
- There are short programs in column A; the first three illustrate our three approaches to implementing lists (functional, mutable with `EmptyList`, mutable with `null`). The fourth uses a new kind of list which we will learn about in this lecture.
- The corresponding environment and heap contents are in columns B through H. Each row contains a single object. References from one object to another are represented by cell names (so the object corresponding to the name `funcList` is stored in spreadsheet cell `E5`, as noted in cell `C2`)
- Columns J and K go into an additional level of detail, illustrating that actual computer memory is simply a sequence of numbered slots (labeled with @), each containing one piece of primitive data or another slot number.

Take a look at these heap layouts and note questions. We'll start class in breakouts to discuss these diagrams and the implications they might have for how we could optimize the `get` operation on lists.

*The idea for using spreadsheets to show how heaps are laid out is due to a student from Spring 2021 -- this is just one example of why Kathi values talking to students after lecture or in office hours!*