# Class 33: Vectors

Back to Implementations of Lists. On to Project Discussion.

Held Thursday, October 28, 1999

Overview

Notes

Contents

Handouts

Summary

## Implementing Lists with Cons Cells

• Yesterday, we saw that it is possible to build cons cells (as in the beloved ``box and arrow'' diagram from 151) in Java.
• Cons cells are often called nodes.
• But how do we implement container-like lists using those cells?
• We can build a wrapper class to provide that functionality.
• The wrapper class will contain an underlying cons-based list to which it adds functionality.
• That list is one of the fields of the class.
• What fields will we need?
• The cons cell at the front of the list.
• The cons cell that serves as the ``current'' element (so that we can iterate through the list).
• The length, since it's easier to keep track of the length than it is to recompute it.
• Others?
• What constructors will we need?
• One for creating the empty list.
• Others?
• What public methods will we need? The ones for simple lists (or whichever type of list we choose to implement).
• How will we implement those methods? We'll consider them one-by-one.
• Here's a start.
```  // +--------+--------------------------------------------------
// | Fields |
// +--------+

public class SimpleDynamicList {
/**
* The length of the list.  It's easier to keep track of this
* than to recompute it each time that it's needed.
*/
int length;

/**
* The elements of the list.  When the list is empty, this is
* null.  Otherwise, it is the cons cell at the front of the list.
*/
ConsCell front;

/**
* The current element of the list.  Used to support iteration.
*/
ConsCell current;

// ...

// +---------+-------------------------------------------------
// | Methods |
// +---------+

/**
* Determine whether there is space available.  See the
* interface for preconditions and postconditions.
*/
public boolean spaceAvailable() {
return true;
} // spaceAvailable()

/**
* Add an element to the list.  See the interface for
* preconditions and postconditions.
*/
// It's easiest to add the element at the front of the
// list.  We can do so by creating a new cons cell and
// putting it at the front of the list.
this.front = new ConsCell(element, this.front);

/**
* Get the next element.  See the interface for preconditions
* and postconditions.
*/
public Object nextElement()
throws Exception
{
if (current == null) {
throw new Exception("No more elements!");
}
// Extract the element at the head of the current sublist.
current = current.tail;
// That's it, we're done.
return returnMe;
} // nextElement

// ...

} // class SimpleDynamicList
```

## Growing Array-Based Lists

• When we compare array-based lists to node-based lists, we see one significant advantage to node-based lists: node-based lists can expand.
• Can we also make array-based lists expand? Certainly. We can build a new array and copy to the new array.
• We'll look at some code.

## History

Tuesday, 10 August 1999

• Created as a blank outline.

Wednesday, 27 October 1999

• Filled in some details.

Thursday, 28 October 999

• Filled in the rest of the details.

Monday, 1 November 1999

• Moved the section on vectors elsewhere.

Monday

Back to Implementations of Lists. On to Project Discussion.

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.