Presentation is loading. Please wait.

Presentation is loading. Please wait.

Professor John Peterson

Similar presentations


Presentation on theme: "Professor John Peterson"— Presentation transcript:

1 Professor John Peterson
CS 280 Data Structures Professor John Peterson

2 Next Project Merge Sort. Read about this and be ready to talk about it Friday.

3 Example 5 null 7 Next Data a b

4 Example 5 null 7 Link Data a b
Link<Integer> a = new Link<Integer>() a.data = 5 Link<Integer> b = new Link<Integer>() b.data = 7 a.next = b

5 Example 5 7 null 3 Link Data a b Evaluate a.next a.data a.next.data
b.next.next a.next.next.data

6 Example 5 7 null 3 Next Data a b What does this do? b.next.next = a;
b = b.next; a.data = a.data + 1; b.data = b.data + b.next.data; a = b.next;

7 Aliasing Exercises Link<Integer> a = new Link<Integer> (); Link<Integer> b = new Link<Integer> (); Link<Integer> c = new Link<Integer> (); Link<Integer> d = a; a.data = 2; b.data = 3; c.data = 4; d.data = 5; a.next = b; b.next = c; c.next = a; a.next.data = 6; b.next.next.data = 7; print(d.data);

8 Avoiding Aliasing One style of programming avoids issues involving aliasing: non-destructive (functional) programming. Once you create structure, you never change it. This allows you to ignore the difference between objects and values

9 Embracing Aliasing If aliasing is present, you need to know that changes are seen by all objects sharing a reference (pointer) to an object. This might be just what you want! You need to be VERY aware of where pointers point.

10 Basic Recursive Techniques
Decide what information you need at each step – if you need to initialize a new value, this requires an auxiliary function. Develop a test to determine whether the recursion is done Update parameters at the recursive calls.

11 Auxiliary Functions These are an IMPORTANT part of recursion! No nice way to do these in Java – place these in the class next to the recursive functions and make them private (why?)

12 Example Lets write a method to create a list of N copies of some value. Which is easier to read / write? Harder: Create a list containing integers from 1 to N. Note the use of an auxiliary function in the recursive version! Note the special case in the iterative version.

13 Duplicate public Link<Integer> duplicate(int n, int v) { if (n <= 0) return null; return new Link<Integer>(v, duplicate(n-1, v)) }

14 upto public Link<Integer> upto(int n) { return upto1(1, n); } public Link<Integer> upto1(int i, int n) { if (i > n) return null; return new Link<Integer>(i, upto1(i+1, n)) }


Download ppt "Professor John Peterson"

Similar presentations


Ads by Google