---
# System prepended metadata

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

---

---
tags: Setup
---

# 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.

We haven't talked in detail about how much *space* each of these approaches uses in memory (or about the space needs of lists in general). This lecture will look at how to optimize list implementations for space.

## 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.**

[This Google Sheet](https://docs.google.com/spreadsheets/d/11X3_q9tN0MfDBQC5C4S7H-cgiVtTbuHkA_2LxcRyBnk/edit?usp=sharing) shows how list objects are laid out in memory, for each of our three list implementations above. Columns A and B are the environment, while columns D through F are the heap. Column D contains the class from which the object came, while columns E and F contain the fields (each row represents an object).

We are using the cell names (like D3, D4) to stand for locations in memory. If a cell contains the name of another cell, like D5, then that field refers to the object in cell D5.

In columns H and I are notes about how many cells were needed to represent each list.

Take a look at these heap layouts and the space they use. Starting in row 20 is an entirely new possible layout of lists in memory: this seems to take much less space that the three versions we have done so far. In class, we will implement this new 4th version. Just familiarize yourself with the layout for now, so that you know what we are trying to build in class.

*The idea for using spreadsheets to show how heaps are laid out is due to one of your classmates this semester -- if you found this helpful, thanks to him!*