[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Exams] [Examples] [Book] [Tutorial] [API]
Back to Data Structures. On to Discussion of Exam 2.
Held Friday, October 15, 1999
Overview
Today we will consider the design of a particular data structure: the list. Surprisingly, there are a number of perspectives on lists.
Notes
Contents
Summary
cons: Create a list, given a head and a tail.
car and head: Get the first element
of the list.
cdr and tail: Get all but the first
element of the list.
setcar! (boo! hiss!): Change the first element
of the list.
setcdr! (boo! hiss!): Change the rest of the list.
head and tail).
cons is the constructor.
car and cdr get the two fields.
setcar! and setcdr! set the two fields.
/**
* A simple implementation of lists, similar to the lists provided
* by languages like Scheme and LISP. Created as an example for
* CSC152.
*
* @author Samuel A. Rebelsky
* @version 1.0 of October 1999
*/
public class SchemeList {
// +--------+--------------------------------------------------
// | Fields |
// +--------+
/** The first element in the list. */
protected Object head;
/** The remainder of the list. */
protected SchemeList tail;
// +--------------+--------------------------------------------
// | Constructors |
// +--------------+
/** Build a new list with specified head and tail. */
public SchemeList(Object head, SchemeList tail) {
this.head = head;
this.tail = tail;
} // SchemeList(Object, SchemeList)
// +-----------------------------------------------------------
// | 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 SchemeList 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(SchemeList newTail) {
this.head = newTail;
} // setTail(SchemeList)
} // class SchemeList
(a b c), we might write
SchemeList c = new SchemeList("c", null);
SchemeList bc = new SchemeList("b", c);
SchemeLists abc = new SchemeList ("a", bc);
SchemeList abc =
new SchemeList("a",
new SchemeList("b",
new SchemeList("c",
nil)));
toString method.
map, length, member,
.....
map.
tail
of a list and then modify that tail, do we modify the original list?
// Assume l is the list (aardvark zebra bison chipmunk)
SchemeList lprime = l.tail();
lprime.setHead("macaque");
out.println(l.tail().head().toString());;
SchemeList class.
// Assume l is the list (aardvark zebra bison chipmunk)
SchemeList lprime = l.tail();
lprime.remove("bison");
out.println(l.contains("bison"));
Tuesday, 10 August 1999
Friday, 15 August 1999
Back to Data Structures. On to Discussion of Exam 2.
[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.30.html
Source text last modified Fri Oct 15 09:26:44 1999.
This page generated on Fri Oct 15 09:30:34 1999 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu