[Instructions] [Search] [Current] [News] [Syllabus] [Glance] [Links] [Handouts] [Project] [Outlines] [Labs] [Assignments] [Quizzes] [Exams] [Examples] [EIJ] [JPDS] [Tutorial] [API]
Back to Introduction to Lists. On to Lab: Animation.
Held Wednesday, March 15, 2000
Overview
Today we consider a variety of issues pertaining to lists.
Notes
http://www.supernews.com/), which has agreed to a free trial
of its Usenet service, starting now and continuing through the coming
weekend. To connect, point your news reader at corp.supernet.com
(207.126.101.100). Please let Mr. Stone know what you think.
Contents
Summary
null.
/**
* A simple implementation of lists, similar to the lists provided
* by languages like Scheme and LISP. Created as an example for
* CSC152. Note that this differs from cons cells in Scheme in
* that it requires that the tail be another cons cell (or nil).
*
* @author Samuel A. Rebelsky
* @version 1.1 of October 1999
*/
public class ConsCell {
// +--------+--------------------------------------------------
// | Fields |
// +--------+
/** The first element in the list. */
protected Object head;
/** The remainder of the list. */
protected ConsCell tail;
// +--------------+--------------------------------------------
// | ConsCelltructors |
// +--------------+
/** Build a new list with specified head and tail. */
public ConsCell(Object head, ConsCell tail) {
this.head = head;
this.tail = tail;
} // ConsCell(Object, ConsCell)
// +-----------------------------------------------------------
// | Extractors |
// +------------+
/**
* Get the head of the list.
* Pre: The list is initialized and nonempty.
* Post: Returns the first element of the list.
* Post: Does not modify the list.
*/
public Object head() {
return this.head;
} // head()
/**
* Get the tail of the list.
* Pre: The list is initialized and nonempty.
* Post: Returns all but the first element of the list (as a list).
* Post: Does not modify the list.
*/
public ConsCell tail() {
return this.tail;
} // tail()
// +-----------+-----------------------------------------------
// | Modifiers |
// +-----------+
/**
* Set the first element of the list to newHead.
* Pre: The list is initialized.
* Post: Subsequent calls to head return newHead (until the
* head is updated again).
* Post: The remainder of the list is not modified.
*/
public void setHead(Object newHead) {
this.head = newHead;
} // setHead(Object)
/**
* Set the remainder of the list to newTail.
* Pre: The list is initialized.
* Post: Subsequent calls to tail return newTail (until the
* tail is updated again).
* Post: The head of the list is not modified.
*/
public void setTail(ConsCell newTail) {
this.head = newTail;
} // setTail(ConsCell)
// +----------------+------------------------------------------
// | Static Methods |
// +----------------+
/**
* Create a new cons cell with a head and tail
* Pre: h is non-null
* t was created by cons or nil
* Post: Returns a new cons cell.
*/
public static ConsCell cons(Object head, ConsCell tail)
{
return new ConsCell(head,tail);
} // cons(Object, ConsCell)
/**
* Determine if a cons cell represents an empty list. In
* this implementation, we use null to represent the empty
* list.
*/
public static boolean isEmpty(ConsCell list)
{
return list == null;
} // isEmpty(ConsCell)
/**
* Create a new empty list.
* Pre: None
* Post: Returns the object used to represent the empty list.
*/
public static ConsCell nil()
{
return null;
} // nil()
} // class ConsCell
(a b c), we might write
ConsCell c = new ConsCell("c", null);
ConsCell bc = new ConsCell("b", c);
ConsCells abc = new ConsCell ("a", bc);
ConsCell abc =
new ConsCell("a",
new ConsCell("b",
new ConsCell("c",
null)));
ConsCell abc =
ConsCell.cons("a", ConsCell.cons("b", ConsCell.cons("c",
ConsCell.nil())));
toString method.
map, length, member,
.....
/**
* The start of an implementation of Simple Lists, using arrays
* as the underlying implementation structure.
*
* @author Samuel A. Rebelsky
* @version 1.0 of March 1999
*/
public class ArrayBasedSimpleList
implements SimpleList
{
// +--------+--------------------------------------------------
// | Fields |
// +--------+
/** The elements of the list. */
protected Object[] elements;
/** The cursor. */
protected int cursor;
/**
* The length of the list (different from the length of
* the array). Also used as the index of the next element
* to add.
*/
protected int length;
// +--------------+--------------------------------------------
// | Constructors |
// +--------------+
/** Create a new list of specified capacity. */
public ArrayBasedSimpleList(int capacity) {
elements = new Object[capacity];
reset();
} // ArrayBasedSimpleList(int)
// +---------+-------------------------------------------------
// | Methods |
// +---------+
/**
* Add an element to the list. See the interface for
* preconditions and postconditions.
*/
public void add(Object element) {
// Add the element.
this.elements[this.length] = element;
// Increase the length
++this.length;
} // add(Object)
/**
* Delete an element from the list. See the interface for
* preconditions and postconditions.
*/
public void delete(Object element) {
// Step through the list until we find an equal element
for (int i = 0; i < this.length; ++i) {
// Note that we use element.equal because some
// elements of the list may be null
if (element.equals(this.elements[i])) {
// Found it!
// Put the last thing here.
this.elements[i] = this.elements[this.length-1];
// Clear the last thing.
this.elements[this.length-1] = null;
// Update the length.
--this.length;
// And we're done.
return;
}
} // for
// Nope, didn't find it. Nothing else to do.
} // delete(Object)
/**
* Determine the length of the list. See the interface
* for preconditions and postconditions.
*/
public int length() {
return this.length;
} // length()
/**
* Determine whether there is space available. See the
* interface for preconditions and postconditions.
*/
public boolean spaceAvailable() {
// There is space available if the length of the list is
// less than the length of the array.
return this.length < this.elements.length;
} // spaceAvailable()
/**
* Reset iteration. See the interface for preconditions and
* postconditions.
*/
public void reset() {
this.cursor = 0;
} // reset()
/**
* Get the next element. See the interface for preconditions
* and postconditions.
*/
public Object nextElement() {
// Note that it is possible to express this more concisely as
// return this.elements[this.cursor++];
// Get the element to return.
Object tmp = this.elements[this.cursor];
// Advance the cursor.
++this.cursor;
// Return the element.
return tmp;
} // nextElement()
/**
* Are there more elements left? See the interface for preconditions
* and postconditions.
*/
public boolean hasMoreElements() {
return this.cursor <= this.length;
} // hasMoreElements()
} // class ArrayBasedSimpleList
Tuesday, 18 January 2000
Wednesday, 15 March 2000
Wednesday, 5 April 2000
Back to Introduction to Lists. On to Lab: Animation.
[Instructions] [Search] [Current] [News] [Syllabus] [Glance] [Links] [Handouts] [Project] [Outlines] [Labs] [Assignments] [Quizzes] [Exams] [Examples] [EIJ] [JPDS] [Tutorial] [API]
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.
This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS152/2000S/Outlines/outline.31.html
Source text last modified Wed Apr 5 09:05:46 2000.
This page generated on Wed Apr 5 09:53:43 2000 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu