Download presentation
Presentation is loading. Please wait.
1
CS 280 Data Structures Professor John Peterson
2
Examples N copies?
3
Examples LI copies(int n, int v) { if (n <= 0) return null; return new LI(v, copies(n-1, v)) Let’s use LI to mean Link
4
Examples upto?
5
Examples LI upto(int n) { return upto1(1,n);} LI upto1(int i, int n) { if (i > n) return null; return new LI(i, upto1(i+1, n)) }
6
Exercises We’ll do these on the board in pairs. Sum a list of integers: int sumLI(LI l) Search a list of integers for a given value: boolean searchLI(int v, LI l) As above, except return the position: whereLI – use “-1” as the magic cookie for “not found”. int whereLI(int v, LI l); Compare two lists of integers: boolean equalsLI(LI 1, LI 2) Append two lists, destructive: LI appendLId(LI x, LI y) Append two lists, non-destuctive: LI appendLI(LI x, LI y)
7
Returning Two Values We’ll be creating functions that return more than one value. We could create a custom “pairing” class that is specific to the classes we need to return, but there’s a better way. How can we use a class to return a pair? How can we make this generic?
8
Generic Pairs We’re going to need a new Generic function class: public class Pair { public T first; public S second; public Pair(T first, S second) { this.first = first; this.second = second; }}
9
Quiz
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.