Download presentation
Presentation is loading. Please wait.
Published byAnnette Wheller Modified over 10 years ago
1
Hw #6 due on 3/25… Java, from the details to the bigger picture Spamventure! From the details, the big (ASCII) picture after break… emanning msheely, 1432 lines!! slam mnaval, 859 lines! further adventures in cs60 !
2
Hw #6 due on 3/25… Java, from the details to the bigger picture after break… emanning msheely, 1432 lines!! slam mnaval, 859 lines! further adventures in cs60 !
3
Big Java…
4
huge libraries of classes (data structures)
5
Even Math add Prof. Williams!
6
Java structures data
7
List L; mySizemyHead List myFirstmyRest "a" myFirstmyRest "c" null myFirstmyRest "b" 3 Java structures data ListNode Singly-linked list data structure L.addToFront("c"); L.addToFront("b"); L.addToFront("a"); List L L = new List();
8
List L; Java structures data List L null I guess this reference is a null space… All objects are handled by reference. Empty references are null.
9
List L; mySizemyHead List 0 Java structures data List L L = new List(); null int ListNode
10
List L; mySizemyHead List myFirstmyRest "c" null 1 Java structures data ListNode L.addToFront("c"); List L L = new List();
11
List L; mySizemyHead List myFirstmyRest "c" null myFirstmyRest "b" 2 Java structures data ListNode L.addToFront("c"); L.addToFront("b"); List L L = new List();
12
List L; mySizemyHead List myFirstmyRest "a" myFirstmyRest "c" null myFirstmyRest "b" 3 Java structures data ListNode L.addToFront("c"); L.addToFront("b"); L.addToFront("a"); List L L = new List();
13
mySizemyHead List myFirstmyRest "a" myFirstmyRest "c" null myFirstmyRest "b" 3 List class ListNode ListNode class !
14
addToFront public void addToFront( String str ) { ListNode LN = new ListNode( str, null ); // 1 LN.myRest = this.myHead; // 2 this.myHead = LN; // 3 this.mySize += 1; // 4 } public void addToFront( String str ) { this.myHead = new ListNode( str, myHead ); // 1-3 this.mySize += 1; // 4 L.addToFront("a"); whoa! same thing:
15
removeFirst L.removeFirst( ); mySizemyHead List myFirstmyRest "c" null myFirstmyRest "b" 2 ListNode before after L List (before) intListNode
16
removeFirst L.removeFirst( ); mySizemyHead List myFirstmyRest "c" null myFirstmyRest "b" 2 ListNode before mySizemyHead List 1 after myFirstmyRest "c" null ListNode L List (before) L List (after) intListNode intListNode return value: ?
17
removeFirst L.removeFirst( ); mySizemyHead List myFirstmyRest "c" null myFirstmyRest "b" 2 ListNode before mySizemyHead List 1 after myFirstmyRest "c" null ListNode L List (before) L List (after) intListNode intListNode return value:
18
Quiz public ListNode removeFirst( ) { ListNode result = if return result; } (1) What is the result? (2) We need an if – why?! (3) Fix the list up … Write the removeFirst method… Name(s): ________________________ Step (3) is one to remember! (4) Return.
19
Quiz public ListNode removeFirst( ) { ListNode result = return result; } (1) Cut the node out and give it a name. (2) "Fix up" the List ( this object) (…) Return the node you cut out. (3) What have we forgotten?! Write the removeFirst method… Try this on the back page first… Step (3) is one to remember!
20
How many? Why have both the mySize field and a length() method?
21
How many? checks the length by actually walking the list (1) Declare + initialize a "runner" variable (2) Test! (4) Update + go back to step 2 (3) Loop body! for loop!
22
How many? checks the length by actually walking the list (1) Declare + initialize a "runner" variable (2) Test! (4) Update + go back to step 2 (3) Loop body! while loops do the same four things…
23
this List Walking the list… (1) Declare + initialize a "runner" variable (2) Test! (4) Update + go back to step 2 (3) Loop body!
24
(1) Declare + initialize a "runner" variable (2) Test! (4) Update + go back to step 2 (3) Loop body! this node List ListNode count int 0 Heap vs. stack ? What is node pointing to??
25
(1) Declare + initialize a "runner" variable (2) Test! (4) Update + go back to step 2 (3) Loop body! this node List ListNode count int 0
26
(1) Declare + initialize a "runner" variable (2) Test! (4) Update + go back to step 2 (3) Loop body! this node List ListNode count int 1
27
(1) Declare + initialize a "runner" variable (2) Test! (4) Update + go back to step 2 (3) Loop body! this node List ListNode count int 1 What is node now pointing to??
28
(1) Declare + initialize a "runner" variable (2) Test! (4) Update + go back to step 2 (3) Loop body! this node List ListNode count int 1
29
(1) Declare + initialize a "runner" variable (2) Test! (4) Update + go back to step 2 (3) Loop body! this node List ListNode count int 2
30
(1) Declare + initialize a "runner" variable (2) Test! (4) Update + go back to step 2 (3) Loop body! this node List ListNode count int 2
31
(1) Declare + initialize a "runner" variable (2) Test! (4) Update + go back to step 2 (3) Loop body! this node List ListNode count int 2
32
(1) Declare + initialize a "runner" variable (2) Test! (4) Update + go back to step 2 (3) Loop body! this node List ListNode count int 3
33
(1) Declare + initialize a "runner" variable (2) Test! (4) Update + go back to step 2 (3) Loop body! this node List ListNode count int 3 What will node now point to??
34
(1) Declare + initialize a "runner" variable (2) Test! (4) Update + go back to step 2 (3) Loop body! this node List ListNode count int 3 Nothing! null the loop exits, returning count …
35
this List Walking the list… (1) Declare + initialize a "runner" variable (2) Test! (4) Update + go back to step 2 (3) Loop body! too slow!
36
this List Walking the list… (1) Declare + initialize a "runner" variable (2) Test! (4) Update + go back to step 2 (3) Loop body! Sprinting
37
(1) Declare + initialize a "runner" variable (2) Test! (4) Update + go back to step 2 (3) Loop body! this node List ListNode count int 0
38
(1) Declare + initialize a "runner" variable (2) Test! (4) Update + go back to step 2 (3) Loop body! this node List ListNode count int 0
39
(1) Declare + initialize a "runner" variable (2) Test! (4) Update + go back to step 2 (3) Loop body! this node List ListNode count int 1
40
(1) Declare + initialize a "runner" variable (2) Test! (4) Update + go back to step 2 (3) Loop body! this node List ListNode count int 1
41
(1) Declare + initialize a "runner" variable (2) Test! (4) Update + go back to step 2 (3) Loop body! this node List ListNode count int 1
42
(1) Declare + initialize a "runner" variable (2) Test! (4) Update + go back to step 2 (3) Loop body! this node List ListNode count int 2
43
(1) Declare + initialize a "runner" variable (2) Test! (4) Update + go back to step 2 (3) Loop body! this node List ListNode count int 2
44
(1) Declare + initialize a "runner" variable (2) Test! (4) Update + go back to step 2 (3) Loop body! this node List ListNode count int 2
45
(1) Declare + initialize a "runner" variable (2) Test! (4) Update + go back to step 2 (3) Loop body! this node List ListNode count int 3
46
(1) Declare + initialize a "runner" variable (2) Test! (4) Update + go back to step 2 (3) Loop body! this node List ListNode count int 3 Done! null the loop exits, returning count …
47
More loops: toString Racket style!
48
More loops: get "Big errors" are handled in Java by throwing exceptions loops until k == pos
49
More loops: equals loops for the full list
50
add L.add( "d" ); mySizemyHead List myFirstmyRest "c" null myFirstmyRest "b" 2 ListNode before adds to the end of the List…
51
L.add( "d" ); mySizemyHead List myFirstmyRest "c" null myFirstmyRest "b" 2 ListNode before mySizemyHead List myFirstmyRest "c" myFirstmyRest "b" 3 ListNode after myFirstmyRest "d" null ListNode add adds to the end of the List…
52
Try it! add public void add( String str ) { ListNode LN = new ListNode(, ); } (1) Handle the empty case (2) If nonempty, write a loop ! (3) How far to loop? (4) and when the loop ends…? (0) Make a new ListNode
53
"Base Case" L.add( "b" ); mySizemyHead List 0 before mySizemyHead List 1 after myFirstmyRest "b" null ListNode null this List this List
54
Try it! (1) Handle the empty case (2) If nonempty, write a loop ! (3) How far to loop? (4) and when the loop ends…? (0) Make a new ListNode public void add( String str ) { ListNode end = new ListNode(, ); if (this.myHead == null) { } else { } Write the add method, which adds a new ListNode at the end.
55
this node List ListNode Our example while loop… (1) You need a different test! (2) What do you need in the loop and after it?
56
Try it! (1) Handle the empty case (2) If nonempty, write a loop ! (3) Loop until node.myRest == null (4) Add end at end of loop. (0) Make a new ListNode public void add( String str ) { ListNode end = new ListNode( str, null ); if (this.myHead == null) { this.myHead = end; } else { ListNode node = this.myHead; while (node.myRest != null) { node = node.myRest; } node.myRest = end; } this.mySize += 1; } Write the add method, which adds a new ListNode at the end. (5) Don't forget!
57
Lists vs. Arrays… mySizemyHead List L myFirstmyRest "c" myFirstmyRest "b" 3 ListNode List myFirstmyRest "d" null ListNode L
58
Lists vs. Arrays… mySizemyHead List L myFirstmyRest "c" myFirstmyRest "b" 3 ListNode List myFirstmyRest "d" null ListNode a single, set length 1 type of element indexing ~ Python no built-in slicing traverse with loops… L Array length A[0] String[] A 3 A[1]A[2] "c" "b" "d" A We should end with Arrays' upside…
59
Lists vs. Arrays… Let's scrapbook it! I cherish my Java!
60
other methods in hw6pr2 split merge ( "d" "b" "e" "c" "a" ) ( "d" "b" "e" )( "c" "a" ) ( "b" "d" "e" )( "a" "c" ) ( "a" "b" "c" "d" "e" )
61
Mergesort! split merge ( "d" "b" "e" "c" "a" ) ( "d" "b" "e" )( "c" "a" ) ( "b" "d" "e" )( "a" "c" ) ( "a" "b" "c" "d" "e" ) What has to happen within these pink arrows?
62
Mergesort! split merge ( "d" "b" "e" "c" "a" ) ( "d" "b" "e" )( "c" "a" ) ( "b" "d" "e" )( "a" "c" ) ( "a" "b" "c" "d" "e" ) mergesort
63
After break? Java Data-structuring details Applications plus, lots more data-structuring details
64
Prof Lewis! Pixels! The week after spring break…
65
Have a great spring break! My plans are for maximum Java!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.