SUNY Geneseo Department of Computer Science


Correctness of List Algorithms, Part 2

{Date}

CSci 141, Fall 2003
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture


Misc

CS Dept. Halloween party

Questions?

Extended list lab, recurrence relation for everyOtherItem

Correctness of List Algorithms

    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 ) )
                }
            }
        }

    }

Search: returns true if x is in the list, returns false if x is not in list

Delete: result contains no occurrences of x

Devil's advocate: "better" delete

    delete( x )
        return new DataList()

The Problem with Searching Lists

Theta(n) search time is really pretty bad in a big data set

Can we devise a data structure that supports faster searches?


Next Lecture