SUNY Geneseo Department of Computer Science


List Algorithms

{Date}

CSci 141, Spring 2005
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture


Misc

Hand out Lab 8

Questions?

List Implementation

Sections 11.3 and 11.4

Basic constructor, creates empty list

getFirst produces value

removeItem produces side-effect

getRest, maybe most important

Methods built from primitive operations often have 3-case structure:

Java syntax question:

    while ( i < 10 )
        i = i + 1;
    while ( i < 10 )
        System.out.println( i );
        i = i + 1;

Operations that might be useful

List Algorithms

Given a list of numbers, return the first odd one (or 0 if the list contains no odd numbers)

    // In class List...
    // Precondition: list contains only integers
    int firstOdd()
        if ! this.isEmpty()
            if this.getFirst() % 2 != 0
                return this.getFirst()
            else
                return this.getRest().firstOdd()
        else
            return 0

Next

Subclasses of List

Read Section 11.5 through subsection 11.5.2


Next Lecture