SUNY Geneseo Department of Computer Science


Lists as Data Sets, Part 1

{Date}

CSci 141, Fall 2003
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture


Misc

Hand out Lab 9

Questions?

Step-count for Lab 8

    findLargest( n )
    --> if n <= 0
            return A[0]
        else if ....

Using Lists

Maintain a list of things to do

    List toDo = new List()
    for each task to do today
        toDo = new List( task, toDo )
    while ! toDo.isEmpty()
        toDo.first().do()
            [ do whatever it takes to complete first task ]
        toDo = toDo.rest()

Fill in details of loop as mini-assignment (done above)

List operations

Lists as Data Sets

"Big Three" operations

(Variations on these definitions are common)

Design a DataList subclass of List that provides these operations

    class DataList extends List {

        DataList insert( Object x ) {
            return new DataList( x, this )
        }

        boolean search( Object x ) {
            if ( ! this.isEmpty() ) {
                if ( this.first() == x ) {
                    return true
                }
                else {
                    this = this.rest()
                    this.search( x )
                }
            }
            else {
                return false;
            }
        }

        DataList delete ( Object x ) {
            ...
        }
    }

("search" has a few things that still need to be cleaned up Friday)

Client examples

    DataList d = new DataList()
    d = d.insert( "Hello" )

What is "Object?"

[All Classes Descend from Object]


Next Lecture