Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compsci 201 Linked Lists from A-Z

Similar presentations


Presentation on theme: "Compsci 201 Linked Lists from A-Z"— Presentation transcript:

1 Compsci 201 Linked Lists from A-Z
Owen Astrachan Jeff Forbes October 20, 2017 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

2 Compsci 201, Fall 2017, Linked Lists & More
O is for … Object Oriented Programming with inheritance and classes Open Source Copyright meets the Creative Commons Overflow From arithmetic to stacks: ouch! Occam’s Razor Not just compsci: simple is good 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

3 Compsci 201, Fall 2017, Linked Lists & More
Plan for the Day Midterm and APT Quiz explained What you learn individually from results What we learn in creating class, assessments What the class collectively learns Linked Lists Re-examined Quick review and concentrate on low-level implementations Toward APT-3 and DNA-Linked assignment 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

4 Compsci 201, Fall 2017, Linked Lists & More
Exam We will provide a pathway to help students succeed in 201 Lessons from midterm for students Lessons for midterm for professors Working toward a path to success, measure success in some way that works 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

5 Compsci 201, Fall 2017, Linked Lists & More
APT Quiz Very few technical issues on server side, but one Some issues on student side We have scores from nearly every student Still need to track timing, will be done Monday We still need to run similarity checks on solutions As a group you did very well 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

6 Highlights from Wednesday
How do you remove all occurrences of “simple” from a list of Strings? How do you remove the first element of a list? How do you remove a middle element? How do you remove the last element? Does the kind of list matter? List<>, LinkedList<>, ArrayList<> 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

7 Compsci 201, Fall 2017, Linked Lists & More
Removing One or All Last class: removing list.get(0) N times Is O(N) for a LinkedList Is O(N2) for an ArrayList What about removing all occurrences of target? There is list.remove() in Java API, but … 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

8 Aside: Using The Right Tool
What is a ConcurrentModificationException? How does the enhanced for-loop work? Object iterated over implements Iterable Can’t remove while iterating using loop Idea: something will be missed when shifting 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

9 Compsci 201, Fall 2017, Linked Lists & More
First Try at RemoveAll If we remove “ant” from [“ant”, “bat”, “cat”] ? OK! What about [“ant”, “ant”, “bat”, “cat”], Oh NO! What does .remove do? public List<String> removeAllWrong(String target, List<String> list) { for(int k=0; k < list.size(); k++) { String w = list.get(k); if (w.equals(target)) { list.remove(k); } return list; 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

10 Second Try at RemoveAll
Adjust loop control variable when shifting This fixes the problem, it works, but …. public List<String> removeAllGack(String target, List<String> list) { for(int k=0; k < list.size(); k++) { String w = list.get(k); if (w.equals(target)) { list.remove(k); k -= 1; } return list; 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

11 Compsci 201, Fall 2017, Linked Lists & More
LastTry at RemoveAll Use java.util.Iterator, lists have one Unfortunately, some operations are “optional” Idiom similar to Scanner, why? Interface! public List<String> removeAllIterator(String target, List<String> list) { Iterator<String> iter = list.iterator(); while (iter.hasNext()) { String w = iter.next(); if (w.equals(target)) { iter.remove(); } return list; 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

12 Compsci 201, Fall 2017, Linked Lists & More
Linear Reminder LIFO LIFO, Stack<> Abomination from some perspectives, but get over it? 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

13 Compsci 201, Fall 2017, Linked Lists & More
Linear Reminder FIFO FIFO, Queue<> Queue<String> q = new LinkedList<>(); 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

14 Compsci 201, Fall 2017, Linked Lists & More
Random Reminder GIGO 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

15 Compsci 201, Fall 2017, Linked Lists & More
ArrayList aka Vector How do you implement ArrayList and what is complexity of calling .add N times? Array instance variable: String[] myData When it’s full? Double size, copy, continue Suppose initially size = 1 … … That’s 2047, which is 2*1024-1 Grow to size N? Totall allocated 2*N - 1 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

16 Compsci 201, Fall 2017, Linked Lists & More
WOTO Remove 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

17 Lynn Conway Joined Xerox Parc in 1973
See Wikipedia and lynnconway.com Joined Xerox Parc in 1973 Revolutionized VLSI design with Carver Mead Joined U. Michigan 1985 Professor and Dean, retired '98 NAE '89, IEEE Pioneer '09 Helped invent dynamic scheduling early '60s IBM Transgender, fired in '68

18 Compsci 201, Fall 2017, Linked Lists & More
LinkedList Internals Create an inner Node class Abstractly: two fields: info and pointer to Node Concretely: see next slide In practice: LinkedList uses doubly-linked list Can iterate forward and backwards Overhead for storage: two pointers: prev/next Tradeoff in performance: memory and speed 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

19 Compsci 201, Fall 2017, Linked Lists & More
A Node By Any Other Name Could be an inner class: implementation only Could be public class in a package Often simply a POJO: Plain Old Java Object Has no methods, does have constructor public class Node { String value; Node next; Node(String s, Node link){ value = s; next = link; } 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

20 Compsci 201, Fall 2017, Linked Lists & More
Building linked lists Read file: the big dog, create list: dog>big>the Add new nodes to front of list // Scanner declarations here Node list = null; while (scanner.hasNext()) { list = new Node(scanner.next(), list); } Where is the assignment to a .next field? Let’s rewrite with more code to explore 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

21 Compsci 201, Fall 2017, Linked Lists & More
Building linked lists Read file the big dog, create list: dog>big>the Add new nodes to front of list // Scanner declarations here Node list = null; while (scanner.hasNext()) { String s = scanner.next(); Node temp = new Node(s,null); temp.next = list; list = temp; } Unpacking the single line version can be helpful 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

22 Dissection of add-to-front
List initially empty First node has first word Read word, create node New node added to front Evaluate RHS of = Then assign to LHS list list A B list = new Node(word,list); Node(String s, Node link) { info = s; next = link;}

23 Standard list processing (iterative)
Visit all nodes once, e.g., count them or process them public int size(Node list){ int count = 0; while (list != null) { count += 1; list = list.next; } return count; What changes if we generalize meaning of process? Print nodes? Append “s” to all info fields Standard idiom: loop until null, list = list.next 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

24 Compsci 201, Fall 2017, Linked Lists & More
Removing first "cat" public Node remove(Node list, String s){ Node start = list; while (list.next != null) { if (list.next.value.equals(s)) { list.next = list.next.next; break; } list = list.next; return start; "dog" "cat" "pig" list 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

25 Compsci 201, Fall 2017, Linked Lists & More
Why Header Node? Want every node to have a predecessor Otherwise first node is a special case public ListNode removeAll(String target, ListNode list) { ListNode dummy = new ListNode("",list); ListNode first = dummy; while (dummy.next != null) { if (dummy.next.info.equals(target)) { dummy.next = dummy.next.next; } else { dummy = dummy.next; return first.next; dummy "" list "dog" "cat" "pig" 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

26 Standard list processing (recursive)
Visit all nodes once, e.g., count them public int recsize(Node list) { if (list == null) return 0; return 1 + recsize(list.next); } Base case is almost always empty list: null pointer Must return correct value, perform correct action Recursive calls use this value/state to anchor recursion Recursive calls make progress towards base case Almost always using list.next as argument 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

27 Recursion with pictures
recsize(Node list) return 1+ recsize(list.next) Counting recursively int recsize(Node list){ if (list == null) return 0; return 1 + recsize(list.next); } recsize(Node list) return 1+ recsize(list.next) recsize(Node list) return 1+ recsize(list.next) ptr This has bad animations recsize(Node list) return 1+ recsize(list.next) int result = recsize(ptr); System.out.println(result);

28 Linked list Practice: Copy
What is a list? Empty or not: mirrored in code public Node copy(Node list) { if (null == list) return null; Node first = new Node(list.info,null); first.next = copy(list.next); return first; } Examine code: .next field assigned? Can do this iteratively too, more variables/code Adding at back requires … 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

29 Linked list Practice: Copy II
What is a list? Empty or not: mirrored in code public Node copy(Node list) { if (null == list) return null; return new Node(list.info,copy(list.next)); } Examine code, did we assign to .next field? 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

30 Two, two, two ways to reverse a list!
We want to turn list = ['A', 'B', 'C'] into rev = ['C', 'B', 'A'] We will move one node at a time, no new nodes! Iteratively and recursively rev B A C list 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

31 Compsci 201, Fall 2017, Linked Lists & More
Iteration Step One A We moved ['A'] from list to the front of rev rev = ['A'] and list = ['B', 'C'] What's the next step? Invariant: rev points to reversed of nodes visited so far A rev B C list 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

32 Compsci 201, Fall 2017, Linked Lists & More
Iteration Step One B How do we make progress and maintain invariant? Invariant: rev points to reversed of nodes visited so far What should ['B'] or list.next point to? What happens if we write list.next = rev What should rev point to? And list point to ? A rev B C list 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

33 Compsci 201, Fall 2017, Linked Lists & More
Iteration Step One C Making progress and maintain invariant? temp = list.next (so we don't lose ['C']) list.next = rev (add to front point to ['A']) rev = list (rev points to front, ['B']) list = temp (list updated) A rev B C list 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

34 Loop and method finished
Establish invariant before loop Update and re-establish within loop public void reverse(ListNode front){     ListNode rev = null;     ListNode list = front;     while (list != null) {         ListNode temp = list.next;         list.next = rev;         rev = list;         list = temp;     }     front = rev; // update state! } B C list A rev 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

35 Compsci 201, Fall 2017, Linked Lists & More
Reverse Recursively This is harder to visualize, shorter to write Which method is preferred? Decide yourself Base case: zero or one node list, nothing to do Reversing a one node list: done Believe in the recursion Reverse after first, reconnect Pictures are important! B A C list 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

36 First the whole thing, then dissect it
This method returns the list it totally changed! private ListNode doRev(ListNode list){ if (list == null || list.next == null) return list; ListNode after = doRev(list.next); list.next.next = list; list.next = null; return after; } 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

37 Establishing the base case
Does the base case do the right thing? What if first == null? Or a one node list? private ListNode doRev(ListNode list){ if (list == null || list.next == null) return list; ListNode after = doRev(list.next); list.next.next = list; list.next = null; return after; } public void reverse(){   front = doRev(front); 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

38 After recursive call, before …
list A C B list ??? after private ListNode doRev(ListNode list){ if (list == null || list.next == null) return list; ListNode after = doRev(list.next); list.next.next = list; list.next = null; return after; } 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

39 Programming with raw Linked Lists
When adding or removing nodes Be sure you alter a .next field Typically via re-assignment or call to new Using iteration: keep pointer to first AND current Allow iteration over list, but must keep pointer to front Sometimes create dummy/header node, return dummy.next Recursion is sometimes simpler than iteration Code mirrors structure of data! 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

40 Compsci 201, Fall 2017, Linked Lists & More
WOTO questions Sometimes recursion helps, sometimes not so much, but practice is good 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

41 Compsci 201, Fall 2017, Linked Lists & More
Wordladder Story Ladder from ‘white’ to ‘house’ white, while, whale, shale, … I can do that… optimally My brother was an English major My ladder is 16, his is 15, how? There's a ladder that's 14 words! The key is ‘sough’ Guarantee optimality! QUEUE I heard the puzzle on the way into Duke in the mid 90's. I called my brother and said I'd solve the problem. He said he would too. He used his brain, I wrote a program. Called him an hour alter and said "got it". He said "got it". I said my code was provably optimal and had a 16-word ladder. He said he had 15. WHAT? I added "sough" to dictionary and got a 14-word ladder  10/20/17 Compsci 201, Fall 2017, Linked Lists & More

42 Queue for shortest path
public boolean findLadder(String[] words, String first, String last){ Queue<String> qu = new LinkedList<>(); Set<String> set = new HashSet<>(); qu.add(first); while (qu.size() > 0){ String current = qu.remove(); if (oneAway(current,last)) return true; for(String s : words){ if (! set.contains(s) && oneAway(from,s)){ qu.add(s); set.(s); } } return false; 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

43 Shortest Path reprised
How does Queue ensure we find shortest path? Where are words one away from first? Where are words two away from first? Why do we need to avoid revisiting a word, when? Why do we use a set for this? Why a HashSet? Alternatives? If we want the ladder, not just whether it exists What's path from white to house? We know there is one. All words one-away from first are on Q and are added when first is dequeued/removed first time through loop. Each of these 1-away-from-start words comes off the queue and words that are one-away from these, or 2-away from start, are put on the Q. BUT it's a queue so all 2-away words go on after the 1-away. In general, we remove N-away words before (N+1)-away words because of FIFO structure 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

44 Compsci 201, Fall 2017, Linked Lists & More
Shortest path proof All words one away from start on queue first iteration What is first value of current when loop entered? All one-away words dequeued before two-away See previous assertion, property of queues Two-away before 3-away, … Each 2-away word is one away from a 1-away word So all enqueued after one-away, before three-away Any w seen/dequeued that's n:away is: Seen before every n+k:away word for k >=1! Don't dwell on this, but walk through it as a semi-formal argument, akin to a hand-wavy proof 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

45 Keeping track of ladder
Find w, a one-away word from current Enqueue w if not seen Call map.put(w,current) Remember keys are unique! Put word on queue once! map.put("lot", "hot") map.put("dot", "hot") map.put("hat", "hot") Remind students that we need to keep track of how words are connected to reconstruct the ladder. Keys in a map are unique, but we only put a word on the queue once! Why? Using a set. We can make this one time the time we make a word the key in a map. The value is what caused the word to be put onto the queue, see examples 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

46 Reconstructing Word Ladder
Run WordLaddersFull See map and call to map.put(word,current) What about when returning the ladder, why is the returned ladder in reverse order? What do we know about code when statement adding (key,value) to map runs? Run program with "white" "house" and with "voter" "crazy" and with "voter" "smart" 10/20/17 Compsci 201, Fall 2017, Linked Lists & More


Download ppt "Compsci 201 Linked Lists from A-Z"

Similar presentations


Ads by Google