Lect15B_lists_recursion

Lect15B_lists_recursion - ITI 1121. Introduction to...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ITI 1121. Introduction to Computing II * Marcel Turcotte School of Information Technology and Engineering Version of March 27, 2010 Abstract Recursive list processing (part II) * These lecture notes are meant to be looked at on a computer screen. Do not print them unless it is necessary. Head + tail if ( ... ) { // base case calculate results } else { // general case // pre-processing s = method( p.next ); // recursion // post-processing } E get( int index ) Lets build the recursive method, this requires augmenting the signature of the method E get( int index ) with an additional parameter representing the current element ( p ). private E get( Node<E> p, int index ); If index represents the position of the element with respect to the list starting at p , what is the position of the element in the list starting at p.next ? Yes, its index-1 ! What is the base case? If the index is zero then simply returns p.value . private E get( Node < E > p, int index ) private E get( Node<E> p, int index ) { if ( index == 0 ) { return p.value; } return get( p.next, index-1 ); } What would occur if the initial value of index was larger than the total number of elements in list? index > and p == null . private E get( Node < E > p, int index ) private E get( Node<E> p, int index ) { if ( p == null ) { throw new IndexOutOfBoundsException(); } if ( index == 0 ) { return p.value; } return get( p.next, index - 1 ); } public E get( int index ) public E get( int index ) { if ( index < 0 ) { throw new IndexOutOfBoundsException(); } return get( first, index ); } private E get( Node<E> p, int index ) { if ( p == null ) { throw new IndexOutOfBoundsException(); } if ( index == 0 ) { return p.value; } return get( p.next, index-1 ); } int indexOf( E obj ) The methods indexOf returns the position of the first (left most) occurrence of obj in the list, the first element of the list is the position 0, the method returns-1 if the element is not found in the list. Following the head + tail strategy, the general case will involve a recursive call for the tail of the list: int s = indexOf( p.next, o ); What does s represent? Its the position of o in the list starting at p.next . What is the position of o with respect to the list starting at p ? 1 + s . int indexOf( Node < E > p, E obj ) Combining s (the result of the recursive call) and the current node. if ( p.value.equals( o ) ) { result = 0; } else if ( result == -1 ) { result = s; } else { result = 1 + s; } int indexOf( Node < E > p, E obj ) What is the base case? The smallest list is the empty list, it does not contain the value were looking for, we should return the special value -1, to indicate that a match could not be found....
View Full Document

This note was uploaded on 01/06/2011 for the course ITI 1121 taught by Professor Samaan during the Spring '10 term at University of Ottawa.

Page1 / 54

Lect15B_lists_recursion - ITI 1121. Introduction to...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online