Held Tuesday, April 4, 2000
Overview
Today we consider how to implement Simple Lists using the
Cons Cells that underlie Scheme Lists.
Notes
Contents
Summary
- Let's see what operations you come up with.
- Iteration
- Reset to the front of the list
- Get the current element
- Advance the current element
- Potentially: back up the current element.
- Addition
- In simple lists, ``somewhere in the list''
- In sequenced lists, at particular locations (front, back,
before current, after current)
- Deletion
- In all lists, delete one copy of an element
- In all lists, delete the current element
- In sequenced lists, delete the first or last element
- What happens when you run out of space in the array?
- Traditionally, you disallow the addition of new elements if the
array has no space remaining.
- You might also create a new, bigger, array when you run out of space.
- Advantages:
- Lists can keep expanding
- You don't need to specify the maximum size of the list when you create it.
- ...
- Disadvantages:
- Inserting an element may no longer take constant time
- May still run out of memory
- ...
- Rather than copying elements in when we delete, we might also leave
``gaps'' in the array by setting the deleted position to null.
- When we add an element, we might put it in the first space
(or some space).
- When iterating, we need to skip over spaces.
- How can we implement the ``other'' kinds of lists we came
up with (simple, sequenced, and sorted).
- There are two basic techniques:
- We can use things a lot like cons cells. Lists implemented
in this way are called linked lists.
- We can use an array behind the scenes. Lists implemented
in this way are called array-based lists. We've
looked at array-based lists in the past.
- Let's see how we can use cons cells to implement simple lists.
In effect, we create a wrapper around scheme lists.
- Standard terminology suggests that we call the cons cells
``nodes''.
- We begin by listing the core methods:
- Create a list
- Add an element ``somewhere in the list'' (close to the general
collection operation)
- Delete one copy of a specified element from the list
- Reset the cursor to the front
- Advance the cursor
- Clear the list
- Get the length of the list
- Is the list empty?
- What fields do we need?
- The first cons cell in the underlying scheme list
- The length of the list
- The last cons cell in the underlying scheme list?
- The current cons cell in the underlying scheme list
- How do we implement the various operations?