k is for compsci 201 maps and linked lists
play

K is for Compsci 201 Maps and Linked Lists Kernel Low-level - PowerPoint PPT Presentation

K is for Compsci 201 Maps and Linked Lists Kernel Low-level foundation for an operating system Key Pairs Public & private key make encryption happening, from Git to SSL Susan Rodger February 19, 2020 2/19/2020 CompSci


  1. K is for … Compsci 201 Maps and Linked Lists • Kernel • Low-level foundation for an operating system • Key Pairs • Public & private key make encryption happening, from Git to SSL Susan Rodger February 19, 2020 2/19/2020 CompSci 201, Spring 2020 1 2/19/2020 CompSci 201, Spring 2020 2 Announcements PFWAE1 • Exam 1 – Do not discuss until with anyone until • Quick Review of Maps handed back • Linked List from high-level to low-level • APT Quiz 1 out today • Similar to how we viewed ArrayList/array • Do by yourself • Low-level linked lists have history and current • Assignment P3 out Friday – due 2/27 pedigree • Builds on P2 Markov • Iterators, Interfaces, Idioms • From design patterns to APIs • APT Quiz ready today, Exam 1 not graded yet 2/19/2020 CompSci 201, Spring 2020 3 2/19/2020 CompSci 201, Spring 2020 4

  2. The java.util.Map interface, concepts • HashMap <Key,Value> or <K,V Method return purpose int # keys map.size() V get value map.get(K) Set<K> Set of keys map.keySet() Collection<V> All values map.values() boolean Is key in Map? map.containsKey(K) map.put(K,V) V (ignored) Insert (K,V) map.entrySet() Set<Map.Entry> Get (K,V) pairs map.clear() void Remove all keys V (ignored) Insert if not there map.putIfAbsent(K,V) 2/19/2020 CompSci 201, Spring 2020 5 2/19/2020 CompSci 201, Spring 2020 6 Investigate Map Solution What is a java.util.List in Java? • One pass over the data instead of many passes • Interface for collection of elements • .Understand all map methods • Add, remove, traverse, … • Why is line 39 never executed? Still needed? • What can a list do to itself? • What can we do to a list? • Why more than one kind of list: Array and Linked? • Useful in different applications • How do we analyze differences? • How do we use them in code? 2/19/2020 CompSci 201, Spring 2020 7 2/19/2020 CompSci 201, Spring 2020 9

  3. Remember? list.remove(0) ArrayList remove(0) is O(N) • What is “faster”? LinkedList or ArrayList • Must shift N-1 elements • Details in code below? Some matter, some … • Shifting N elements is O(N 2 ): why? RemoveFirst 1.4 y = 0.0064x 2 - 0.0156x + 0.0238 1.2 R² = 0.9984 1 0.8 0.6 0.4 0.2 y = -4E-05x + 0.0009 0 linked array Linear (linked) Poly. (array) 2/19/2020 CompSci 201, Spring 2020 10 2/19/2020 CompSci 201, Spring 2020 11 Random Access v Splicing… Conceptual: array[] to Node • How do we implement ArrayList? Use array[] • How does find-a-track work? Fast forward? • list.get(n) is O(1), BUT • Quick survey of linked list code • list.remove(0) is O(N) • How do we implement LinkedList? Use Node • list.get(n) is O(n), BUT • list.remove(0) is O(1) • Tradeoffs: what does sequence of nodes provide? • http://bit.ly/201playRecord 2/19/2020 CompSci 201, Spring 2020 12 2/19/2020 CompSci 201, Spring 2020 13

  4. What’s in a Node? Visualizing/Understanding Nodes • Some information • https://coursework.cs.duke.edu/rodger/diyad-new • diyad.linkedlist.SimpleLinkedList • Place to snap another node • Like pair, note: this not needed below • Instance variables for String and "next node" • In Java we’ll see • String reference: info • Node reference: next 2/19/2020 CompSci 201, Spring 2020 14 2/19/2020 CompSci 201, Spring 2020 15 remove(0) for linked list? Adding nodes to end: .add(..) • Looking at remove(0) not remove(n) now • Instance variables myFirst and myLast • Class invariant: myLast references last node • Initially null, but we'll see what .add does • Symmetry: myFirst references first node • When the list is empty, special case? • Always add node to end of list 2/19/2020 CompSci 201, Spring 2020 16 2/19/2020 CompSci 201, Spring 2020 17

  5. Adding New Nodes Visualizing, Thinking, Understanding • To add to the end of a linked list • How to picture myLast and myLast.next • Maintain reference to first node • Both are Node pointers aka Node references • only through first node can we access entire list • Like all Java Object variables: memory location • Need reference to last node • To add a new last node • Conceptually? An arrow, a pointer • Often need initialization code • References a Node: label for memory location • First node anchors list myFirst myLast • Must do before loop • Loop will add over and over to end 2/19/2020 CompSci 201, Spring 2020 18 2/19/2020 CompSci 201, Spring 2020 19 Only one node in the list? Another program for understanding myFirst? myLast? • Not modeling a List class, just plain Nodes https://coursework.cs.duke.edu/201spring20/classcode/blob/master/src/LowLevelLinkDemo.java • myFirst • LowLevelLinkDemo : add to back, keep front myLast • Local variable last : always point to last node • Each time through loop? True, thus an invariant • a a 2/19/2020 CompSci 201, Spring 2020 20 2/19/2020 CompSci 201, Spring 2020 22

  6. Visualizing Code – CreateList Main in LowLevelLinkDemo.java Add nodes to e end of list • Using Java Tutor: • See first and last: both Node variables 2/19/2020 CompSci 201, Spring 2020 23 2/19/2020 CompSci 201, Spring 2020 24 Adding first node to linked list Visualizing Code – CreateListFront Add nodes to f front of list • Repeatedly add first element, initially null • Using Java Tutor: • New first node points at previous first node • See first and last: both Node variables • first references/points to new first node • Can use first = new Node(vg[k],first) 2/19/2020 CompSci 201, Spring 2020 25 2/19/2020 CompSci 201, Spring 2020 26

  7. Reference: Array Traversal List Traversal • Visiting (printing) every value in an array • Visiting (printing) every value in an array • Initialize index, print w/index, increment index • Start with first node, print .info, advance .next • Elements of array are adjacent in memory • Done when current node is null 2/19/2020 CompSci 201, Spring 2020 27 2/19/2020 CompSci 201, Spring 2020 28 WOTO (correctness counts) John Tukey: 1915-2000 • Cooley-Tukey FFT http://bit.ly/201spring20-0219-1 • Bit is a b inary dig it • Box or Box and Whiskers Plots You can install Java Tutor in IntelliJ – see course Far better an approximate answer to website Resources tab the right question, which is often vague, than an exact answer to the wrong question, which can always be made precise. The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. 2/19/2020 CompSci 201, Spring 2020 29 2/19/2020 CompSci 201, Spring 2020 30

  8. Removing some values: filter Incorrect Results • Remove all occurrences of X, or … • See ListRemoveAndCount.java • When we remove in array, we shift. Trouble? • You shouldn’t do this: results in errors s • Remove the k th element (think 0) • ListRemoveAndCount : exception thrown! • ConcurrentModificationException 2/19/2020 CompSci 201, Spring 2020 31 2/19/2020 CompSci 201, Spring 2020 32 Iterators to the Rescue From Iterator to Iterable • Enhanced for: for(String s : list) { … • Iterators are soooo nice. But timing? • Why O(N) linked list and O(N 2 ) array? • Underneath, uses iterator • Code below O(N) for both lists! 2/19/2020 CompSci 201, Spring 2020 33 2/19/2020 CompSci 201, Spring 2020 34

  9. From Iterator to Iterable Compare the two • What if indexing loop used?, • ListSplicer.java • e.g., list.get(k) • Code below is ? 2/19/2020 CompSci 201, Spring 2020 35 2/19/2020 CompSci 201, Spring 2020 36 APT Practice Quiz WOTO • APT Practice Quiz is on Sakai now http://bit.ly/201spring20-0219-2 • NOT FOR CREDIT, Just for practice • You can see how an APT quiz works • Only Available through Sunday 11:59pm • RECOMMEND trying APT Practice Quiz before taking the APT Quiz1 2/19/2020 CompSci 201, Spring 2020 38 2/19/2020 CompSci 201, Spring 2020 39

  10. APT Quiz Details APT Quiz • APT Quiz 1 available on Sakai • Wed. Feb 19 at 8pm – Mon. Feb 24 11:59pm • We expect that everyone will get the first problem • Once you start, you get 2.5 hours • Sometimes we are wrong. But it’s designed to • You cannot stop and restart it. be straightforward. If you’ve done the APTs? • More time if you get accommodations • Must start by 9:29pm Monday night! You’ll succeed • Recommend you take it BEFORE Monday • You can see the timer in Sakai • We expect everyone will know how to solve the • We will not grade anything you submit after time runs other problems, but sometimes coding and out debugging is not easy • You CANNOT, CANNOT, CANNOT collaborate on the quiz. • There is a time limit, if stuck? Try next problem We run reasonably sophisticated similarity detection software 2/19/2020 CompSci 201, Spring 2020 40 2/19/2020 CompSci 201, Spring 2020 41

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend