SUNY Geneseo Department of Computer Science


List Search and Delete Algorithms

{Date}

CSci 141, Fall 2003
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture


Questions?

Proofs and derivations for lab 9 should be standard induction, recurrence analyses

EveryOtherItem

[Extract Items from Every Other Tail of a List]

Lists as Data Sets

    class DataList extends List {

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

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

        DataList delete ( Object x ) {
            // Postcondition: Result contains no
            // occurrences of x
            if ( this.isEmpty() ) {
                return this
            }
            else {
                if ( this.first().equals(x) )
                    return this.rest().delete( x )
                else {
                    return new DataList( this.first(), this.rest().delete( x ) )
                }
            }
        }
   }

Comments on search

[References to Objects]

Efficiency -- how many primitve List messages do these send? (correlated with execution time)

Mini-assignment: work out number of primitive messages for delete


Next Lecture