[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Exams] [Examples] [Book] [Tutorial] [API]
Back to Discussion of Exam 2. On to Vectors.
Held Wednesday, October 27, 1999
Overview
Today, we will continue our discussion of lists by considering techniques one might use for implementing lists.
Notes
objects[i] vs. get(i) within
your partition methods.
Contents
Handouts
Summary
/**
* 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
/**
* 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!
// Shift everything down.
for (int j = i; j < this.length-1; ++j) {
this.elements[j] = this.elements[j+1];
// Clear the last thing.
this.elements[this.length-1] = null;
// Update the length.
--this.length;
// And we're done.
return;
} // if found
} // for
// Nope, didn't find it. Nothing else to do.
} // delete(Object)
/**
* A simple implementation of lists, similar to the lists provided
* by languages like Scheme and LISP. Created as an example for
* CSC152. Based on the incredibly-similar ConsCell (Samuel
* A. Rebelsky. ConsCells.java. Version 1.0 of October 1999.
* Found on the Web at
* {$site_url}/Examples/ConsCell.java
* Accessed 27 October 1999.)
*
* @author Samuel A. Rebelsky
* @version 1.0 of October 1999
*/
public class ConsCell {
// +--------+--------------------------------------------------
// | Fields |
// +--------+
/** The first element in the list. */
protected Object head;
/**
* The remainder of the list. Set to null when there are
* no more elements in the list.
*/
protected ConsCell tail;
// +--------------+--------------------------------------------
// | Constructors |
// +--------------+
/** 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)
} // class ConsCell
Tuesday, 10 August 1999
Wednesday, 27 October 1999
Back to Discussion of Exam 2. On to Vectors.
[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Exams] [Examples] [Book] [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/99F/Outlines/outline.32.html
Source text last modified Wed Oct 27 12:17:52 1999.
This page generated on Thu Oct 28 09:51:04 1999 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu