compsc sci 201 201 maps ps a and nd link nked l lists
play

Compsc sci 201 201 Maps ps a and nd Link nked L Lists Susan - PowerPoint PPT Presentation

Compsc sci 201 201 Maps ps a and nd Link nked L Lists Susan Rodger February 19, 2020 2/19/2020 CompSci 201, Spring 2020 1 K is for Kernel Low-level foundation for an operating system Key Pairs Public & private


  1. Compsc sci 201 201 Maps ps a and nd Link nked L Lists Susan Rodger February 19, 2020 2/19/2020 CompSci 201, Spring 2020 1

  2. K is for … • Kernel • Low-level foundation for an operating system • Key Pairs • Public & private key make encryption happening, from Git to SSL 2/19/2020 CompSci 201, Spring 2020 2

  3. Announcements • Exa Exam 1 1 – Do no not d dis iscuss unt until w wit ith any h anyone unt until hand handed b bac ack • APT APT Quiz 1 1 out t toda day • Do by yourself • Assig ignm nment nt P P3 out F Frida day – due 2/27 ue 2/27 • Builds on P2 Markov 2/19/2020 CompSci 201, Spring 2020 3

  4. PFWAE1 • Qui uick Revi view of of Maps • Link nked ed List st f from h om high-le level to to l low-leve vel • Similar to how we viewed ArrayList/array • Low-level linked lists have history and current pedigree • Iterators, I Inter erfac aces es, I Idioms • From design patterns to APIs • APT Qui uiz ready today, E Exam 1 1 no not g graded y yet 2/19/2020 CompSci 201, Spring 2020 4

  5. The java.util.Map interface, concepts • Has ashM hMap < <Key,Val alue ue> o 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) V (ignored) Insert (K,V) map.put(K,V) Set<Map.Entry> Get (K,V) pairs map.entrySet() void Remove all keys map.clear() V (ignored) Insert if not there map.putIfAbsent(K,V) 2/19/2020 CompSci 201, Spring 2020 5

  6. 2/19/2020 CompSci 201, Spring 2020 6

  7. Investigate Map Solution • One p ne pass o over er t the he d data ins instead o of many any p passes • .Understand all map methods • Why is line 39 never executed? Still needed? 2/19/2020 CompSci 201, Spring 2020 7

  8. What is a java.util.List in Java? • Interface ace for c collec lectio ion o n of element nts • Add, remove, traverse, … • What can a list do to itself? • What can we do to a list? • Why hy more t than han one ne kin ind o of lis list: A Array and and L Link inked? • Useful in different applications • How do we analyze differences? • How do we use them in code? 2/19/2020 CompSci 201, Spring 2020 9

  9. Remember? list.remove(0) • Wha hat is is “faster”? L Link inkedList o or Arra rrayList 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

  10. ArrayList remove(0) is O(N) • Mus ust s shif hift N N-1 e elem ement ents • Details in code below? Some matter, some … • Shif hifting N N elem elements is is O(N 2 ): wh why? 2/19/2020 CompSci 201, Spring 2020 11

  11. Random Access v Splicing… • How does es f find ind-a-tr track w k work? k? F Fast f st forward? • Quick survey of linked list code • http://bit.ly/201playRecord 2/19/2020 CompSci 201, Spring 2020 12

  12. Conceptual: array[] to Node • How do w do w we impl plement Arra rrayList? U ? Use a arra ray[] • list.get(n) is O(1), BUT • list.remove(0) is O(N) • How do we e im imple lement L Link inkedList? U Use N e Node • list.get(n) is O(n), BUT • list.remove(0) is O(1) • Trad adeo eoffs: w what d does s sequenc uence o of nodes es p provid ide? e? 2/19/2020 CompSci 201, Spring 2020 13

  13. What’s in a Node? • Some i e info forma rmation • Pla lace t to snap nap ano another no node • In n Java w we’ll s ll see ee • String reference: info • Node reference: next 2/19/2020 CompSci 201, Spring 2020 14

  14. Visualizing/Understanding Nodes • https://co ://coursework rk.cs cs.d .duke.ed edu/r /rodg dger/ er/di diyad ad-new • diyad.linkedlist.SimpleLinkedList • Like pair, note: this not needed below • Instance variables for String and "next node" 2/19/2020 CompSci 201, Spring 2020 15

  15. remove(0) for linked list? • Looking ng at at remove(0) not ot remove(n) now • Instance variables myFirst and myLast • Initially null, but we'll see what .add does 2/19/2020 CompSci 201, Spring 2020 16

  16. Adding nodes to end: .add(..) • Class i invar aria iant nt: : myLast refer erenc ences es l las ast n node • Symmetry: myFirst references first node • When hen t the he lis list is is em empty, s spec ecial l case? • Always add node to end of list 2/19/2020 CompSci 201, Spring 2020 17

  17. Adding New Nodes • To ad add to the he end end o of a a link linked lis list • Maintain reference to first node • only through first node can we access entire list • Need reference to last node • To add a new last node • Often n need i initiali ializatio ion c n code • First node anchors list • Must do before re loop • Loop will add over and over to end 2/19/2020 CompSci 201, Spring 2020 18

  18. Visualizing, Thinking, Understanding • How to p pict ctur ure e myLas ast and and my myLast.ne .next • Both are Node pointers aka Node references • Like all Java Object variables: memory location • Conc nceptuall lly? A An n ar arrow, a a point inter • References a Node: label for memory location myFirst myLast 2/19/2020 CompSci 201, Spring 2020 19

  19. Only one node in the list? myFirst? myLast? myFirst myLast • a 2/19/2020 CompSci 201, Spring 2020 20

  20. Another program for understanding • Not m model elin ing a a Lis ist c cla lass, j jus ust p plai lain N Nodes es https://coursework.cs.duke.edu/201spring20/classcode/blob/master/src/LowLevelLinkDemo.java • • LowLevelLinkDemo : add to back, keep front • Local variable last : always point to last node • Each time through loop? True, thus an invari rian ant 2/19/2020 CompSci 201, Spring 2020 22

  21. Main in LowLevelLinkDemo.java 2/19/2020 CompSci 201, Spring 2020 23

  22. Visualizing Code – CreateList Add nodes to end end of list • Using J Java va T Tutor: • See e fir irst and and la last: b both N h Node v e var ariables 2/19/2020 CompSci 201, Spring 2020 24

  23. Adding first node to linked list • Repe peatedl dly a add dd fir irst elem element, init initially null null • New first node points at previous first node • first references/points to new first node • Can use first = new Node(vg[k],first) 2/19/2020 CompSci 201, Spring 2020 25

  24. Visualizing Code – CreateListFront Add nodes to fro ront of list • Using J Java va T Tutor: • See e fir irst and and la last: b both N h Node v e var ariables 2/19/2020 CompSci 201, Spring 2020 26

  25. Reference: Array Traversal • Visit iting ing ( (print nting ing) e ever ery v value i ue in an a array • Initialize index, print w/index, increment index • Elements of array are adjacent in memory 2/19/2020 CompSci 201, Spring 2020 27

  26. List Traversal • Visit iting ing ( (print nting ing) e ever ery v value i ue in an a array • Start with first node, print .info, advance .next • Done when current node is null 2/19/2020 CompSci 201, Spring 2020 28

  27. WOTO (correctness counts) http://bit.ly/201spring20-0219-1 You can i n install J all Java T Tutor i in IntelliJ lliJ – see c course e websit ite R e Resour urces ces t tab 2/19/2020 CompSci 201, Spring 2020 29

  28. John Tukey: 1915-2000 • Cool ooley-Tukey F FFT • Bit it is is a a b ina inary d dig ig it it • Box o or Box and and W Whis hiskers P Plo lots Far better an approximate answer to 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 30

  29. Removing some values: filter • Rem emove all o all occ ccurrences o of X, o , or … • When we remove in array, we shift. Trouble? • ListRemoveAndCount : exception thrown! • ConcurrentModificationException 2/19/2020 CompSci 201, Spring 2020 31

  30. Incorrect Results • See e ListRemoveAnd ndCount unt.j .java • You shouldn’t do this: results in errors • Remove the k th element (think 0) 2/19/2020 CompSci 201, Spring 2020 32

  31. Iterators to the Rescue • Itera rators rs ar are e soooo oooo nic nice. B But ut t tim iming? • Why O(N) linked list and O(N 2 ) array? 2/19/2020 CompSci 201, Spring 2020 33

  32. From Iterator to Iterable • Enhance nced f for: for(String s : list) { … • Underneath, uses iterator • Code below O(N) for both lists! 2/19/2020 CompSci 201, Spring 2020 34

  33. From Iterator to Iterable • Wha hat if if ind indexing lo loop us used?, • e.g., list.get(k) • Code below is ? 2/19/2020 CompSci 201, Spring 2020 35

  34. Compare the two • ListSplice licer.j .java 2/19/2020 CompSci 201, Spring 2020 36

  35. WOTO http:// //bi bit.ly/2 /201spr pring20-02 0219 19-2 2/19/2020 CompSci 201, Spring 2020 38

Recommend


More recommend