Compsci 201 Linked Lists from A-Z

Slides:



Advertisements
Similar presentations
Singly linked lists Doubly linked lists
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Stacks, Queues, and Deques
Stacks, Queues, and Deques
CPS 100, Spring From data to information to knowledge l Data that’s organized can be processed  Is this a requirement?  What does “organized”
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
LISTS Slides of K. Birman, Cornell University. List Overview 2  Purpose  Maintain an ordered collection of elements (with possible duplication)  Common.
Compsci 101.2, Fall PFThursday l Review Organization and Problem-Solving  Defining functions, calling functions  Return types, print, None l.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
Compsci 06/101, Fall How to build a program l Create a module  Can be used by other modules via import  Collection of functions, later collection.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
CompSci 100e 7.1 Plan for the week l Review:  Union-Find l Understand linked lists from the bottom up and top-down  As clients of java.util.LinkedList.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
CS 367 Introduction to Data Structures Lecture 5.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
CompSci 100E 19.1 Getting in front  Suppose we want to add a new element  At the back of a string or an ArrayList or a …  At the front of a string.
Compsci 100, Fall From data to information to knowledge l Data that’s organized can be processed  Is this a requirement?  What does “organized”
CPS 100e 6.1 What’s the Difference Here? l How does find-a-track work? Fast forward?
CPS 100, Spring Interlude for trees l Joyce Kilmer Joyce Kilmer l Balanced Trees  Splay  Red-Black  AVL  B-tree.
Linked Data Structures
Slides by Donald W. Smith
Review Array Array Elements Accessing array elements
CSE 373 Implementing a Stack/Queue as a Linked List
Linked List Introduction
Copy Constructor / Destructors Stacks and Queues
Week 4 - Friday CS221.
CS 215 Final Review Ismail abumuhfouz Fall 2014.
COMP 103 Linked Structures Marcus Frean 2014-T2 Lecture 17
Introduction To Repetition The for loop
Building Java Programs
Plan for Week Linear structures: Stack, Queue
Plan for the Week Review Big-Oh
Compsci 201 Trees and Tradeoffs
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
What’s the Difference Here?
Owen Astrachan Jeff Forbes October 19, 2017
Compsci 6/101: PFTW What is Python? What is a programming language?
Compsci 201 Trees, Tries, Tradeoffs
Top Ten Words that Almost Rhyme with “Peas”
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Announcements Final Exam on August 17th Wednesday at 16:00.
Topic 11 Linked Lists -Joel Spolsky
Stacks, Queues, and Deques
Building Java Programs
Stacks.
Stacks, Queues, and Deques
Linked List (Part I) Data structure.
Building Java Programs
Stacks.
Building Java Programs
Introduction to C++ Linear Linked Lists
CSE 143 Lecture 5 References and Linked Nodes
CSC 143 Java Linked Lists.
Data Structures & Algorithms
Building Java Programs
Stacks, Queues, and Deques
Lecture 7: Linked List Basics reading: 16.2
TCSS 143, Autumn 2004 Lecture Notes
slides created by Ethan Apter
Linked lists Low-level (concrete) data structure, used to implement higher-level structures Used to implement sequences/lists (see CList in Tapestry) Basis.
Topic 11 Linked Lists -Joel Spolsky
CompSci 101 Introduction to Computer Science
Compsci 201, O-Notation and Maps (Interfaces too)
Presentation transcript:

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

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

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

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

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

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

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 … https://coursework.cs.duke.edu/201fall17/list-timing/blob/master/src/ListRemove.java 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

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 https://coursework.cs.duke.edu/201fall17/list-timing/blob/master/src/ListRemove.java 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

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

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

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

Compsci 201, Fall 2017, Linked Lists & More Linear Reminder LIFO LIFO, Stack<> https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html Abomination from some perspectives, but get over it? 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

Compsci 201, Fall 2017, Linked Lists & More Linear Reminder FIFO FIFO, Queue<> https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html Queue<String> q = new LinkedList<>(); 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

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

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 1 + 2 + 4 + 8 + … + 256 + … + 1024 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

Compsci 201, Fall 2017, Linked Lists & More WOTO Remove http://bit.ly/201fall17-oct20-1 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

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

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 http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/LinkedList.java?av=f 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

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

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

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

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;}

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

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

Compsci 201, Fall 2017, Linked Lists & More Why Header Node? https://coursework.cs.duke.edu/201fall17/list-timing/blob/master/src/RawLinked.java 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

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

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);

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

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

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

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

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

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

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

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

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

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

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

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

Compsci 201, Fall 2017, Linked Lists & More WOTO questions http://bit.ly/201fall17-oct20-2 Sometimes recursion helps, sometimes not so much, but practice is good 10/20/17 Compsci 201, Fall 2017, Linked Lists & More

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

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

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

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

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

Reconstructing Word Ladder Run WordLaddersFull https://coursework.cs.duke.edu/201fall17/wordladders/blob/master/src/WordLaddersFull.java 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