Compsci 201 Linked Lists APT Quiz Owen Astrachan

  • Slides: 24
Download presentation
Compsci 201 Linked Lists APT Quiz Owen Astrachan ola@cs. duke. edu October 5, 2018

Compsci 201 Linked Lists APT Quiz Owen Astrachan ola@cs. duke. edu October 5, 2018 10/5/2018 Compsci 201, Fall 2018, Linked Lists 1

J is for … • Java • A simple, object-oriented, distributed, interpreted, robust, secure,

J is for … • Java • A simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high performance, multi-threaded, and dynamic language. • Just in Time Teaching • Introduce concepts when needed, in context of solving problems 10/5/2018 Compsci 201, Fall 2018, Linked Lists 2

PFFFi. O • Linked lists as implementation technique • Nodes as low-level implementation for

PFFFi. O • Linked lists as implementation technique • Nodes as low-level implementation for java. util. Linked. List which is used for … • The java. util. Queue interface. . Coming later • Why nodes are the basis for interview questions • APT Quiz and second chances • Also midterm second chances 10/5/2018 Compsci 201, Fall 2018, Linked Lists 3

Quick Review for Context • Saying no to quadratic, O(N 2) performance • A

Quick Review for Context • Saying no to quadratic, O(N 2) performance • A set implemented with Array. List is slow … • Searching before add is O(N)add is O(1) • That O(1) is amortized over all the. add calls • If N is small this is ok! Idea behind hashing • Array. List (buckets) of Array. List (here N is small) • Array. List (buckets) of Nodes/linked list ok too 10/5/2018 Compsci 201, Fall 2018, Linked Lists 4

Running times in seconds machine: 109 instructions/sec N O(log N) O(N 2) 10 3

Running times in seconds machine: 109 instructions/sec N O(log N) O(N 2) 10 3 E-9 1 E-8 3. 3 E-8 0. 0000001 100 7 E-9 1 E-7 6. 64 E-7 0. 0001 1, 000 1 E-8 1 E-6 0. 00001 0. 001 10, 000 1. 3 E-8 0. 00001 0. 00013 0. 10 100, 000 1. 7 E-8 0. 0001 0. 0017 10. 001 0. 02 16. 7 min 1. 0 29. 9 31. 7 years 1, 000 0. 00000002 1, 000, 000 0. 00000003 10/5/2018 O(N) Compsci 201, Fall 2018, Linked Lists 5

Review: list. remove(0) • What is “faster”? Linked. List or Array. List Remove. First

Review: list. remove(0) • What is “faster”? Linked. List or Array. List Remove. First 1. 4 R 2 = 0. 9984 1. 2 1 0. 8 0. 6 0. 4 0. 2 10/5/2018 00 00 00 15 00 00 00 14 00 13 00 00 Linear(linked) 12 00 0 00 11 00 00 10 0 90 00 0 array 80 00 00 0 70 0 60 00 0 linked 50 00 40 0 30 00 20 10 00 0 0 Poly. (array) Compsci 201, Fall 2018, Linked Lists 6

From Concepts to Java • remove. First(new Array. List<String>()) • remove. First(new Linked. List<String>())

From Concepts to Java • remove. First(new Array. List<String>()) • remove. First(new Linked. List<String>()) • Wouldn’t do this, but we can, why? 10/5/2018 Compsci 201, Fall 2018, Linked Lists 7

WOTO http: //bit. ly/201 fall 18 -oct 5 -1 10/5/2018 Compsci 201, Fall 2018,

WOTO http: //bit. ly/201 fall 18 -oct 5 -1 10/5/2018 Compsci 201, Fall 2018, Linked Lists 8

Josh Bloch • Led design of Java Collections Framework • Formerly Java Chief Architect

Josh Bloch • Led design of Java Collections Framework • Formerly Java Chief Architect at Google • Professor of the Practice CMU APIs should be easy to use and hard to misuse. It should be easy to do simple things; possible to do complex things; and impossible, or at least difficult, to do wrong things. 10/5/2018 Compsci 201, Fall 2018, Linked Lists 9

Review: from array to linked list • How do we implement Array. List and

Review: from array to linked list • How do we implement Array. List and Linked. List? • Low-level implementation details • We use an array[] for Array. List • We use a Node for Linked. List • Why are we using many nodes and not an array? • Same number of nodes as Strings/elements • Not amortized analysis, but exactly N • Remove without shifting 10/5/2018 Compsci 201, Fall 2018, Linked Lists 10

Review: What’s in a Node? • Some information • Place to snap another node

Review: What’s in a Node? • Some information • Place to snap another node • In Java we’ll see • String reference: info • Or int or … • Node reference: next 10/5/2018 Compsci 201, Fall 2018, Linked Lists 11

Connecting Nodes • https: //github. com/astrachano/classcode 201 fall 18 • Constructor and after a

Connecting Nodes • https: //github. com/astrachano/classcode 201 fall 18 • Constructor and after a 9 b 6 c 3 a b c 10/5/2018 Compsci 201, Fall 2018, Linked Lists 12

Connecting Nodes • What is list referenced by c? a • Where is last

Connecting Nodes • What is list referenced by c? a • Where is last node? b c c 10/5/2018 Compsci 201, Fall 2018, Linked Lists 13

Adding New Nodes • To add to the end of a linked list •

Adding New Nodes • To add to the end of a linked list • Need 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 need initialization code • First node anchors list • Must do before loop • Loop will add over and over to end 10/5/2018 Compsci 201, Fall 2018, Linked Lists 14

Adding nodes to end • Loops: Initialize, loop, finalize • Loop invariant: last always

Adding nodes to end • Loops: Initialize, loop, finalize • Loop invariant: last always references last node • Note initialization and update in the loop 10/5/2018 Compsci 201, Fall 2018, Linked Lists 15

Visualizing Code • Using Java Tutor: https: //goo. gl/Euf. Qp 6 • See first

Visualizing Code • Using Java Tutor: https: //goo. gl/Euf. Qp 6 • See first and last: both Node variables 10/5/2018 Compsci 201, Fall 2018, Linked Lists 16

Array Traversal • Visiting (printing) every value in an array • Initialize index, print

Array Traversal • Visiting (printing) every value in an array • Initialize index, print w/index, increment index • Elements of array are adjacent in memory 10/5/2018 Compsci 201, Fall 2018, Linked Lists 17

List Traversal • Visiting (printing) every value in an array • Start with first

List Traversal • Visiting (printing) every value in an array • Start with first node, print. info, advance. next • Done when current node is null 10/5/2018 Compsci 201, Fall 2018, Linked Lists 18

List Traversal FAQ • Nearly universal: while (list != null) • Loop body? list

List Traversal FAQ • Nearly universal: while (list != null) • Loop body? list = list. next; • while(list != null && list. next != null) • After loop? list pointing at last node, but … • To add/change structure of a list • Must assign to a. next field: list. next = … • Otherwise nodes not linked or unlinked 10/5/2018 Compsci 201, Fall 2018, Linked Lists 19

WOTO APT • https: //www 2. cs. duke. edu/csed/newapt/listcount. htm l • How can

WOTO APT • https: //www 2. cs. duke. edu/csed/newapt/listcount. htm l • How can you use List. Node in your code • Could be inner/nested class, but … • First Create/Copy/Paste in same project • What happens if you … • Have an infinite loop? • Misname your method? 10/5/2018 Compsci 201, Fall 2018, Linked Lists 20

Adding first node to linked list • Repeatedly add first element, initially null •

Adding first node to linked list • Repeatedly add first element, initially null • New first node points at old first node • First references/points to new first node • Can use first = new Node(vg[k], first) 10/5/2018 Compsci 201, Fall 2018, Linked Lists 21

Understanding • Note: first is constructor argument • New node created will point at

Understanding • Note: first is constructor argument • New node created will point at first • Then first will reference the new node • or: first = new Node(vg[k], first) nf p first q 10/5/2018 Compsci 201, Fall 2018, Linked Lists 22

WOTO http: //bit. ly/201 fall 18 -oct 5 -2 10/5/2018 Compsci 201, Fall 2018,

WOTO http: //bit. ly/201 fall 18 -oct 5 -2 10/5/2018 Compsci 201, Fall 2018, Linked Lists 23

APT Quiz • There were some technical issues • Still working on those, related

APT Quiz • There were some technical issues • Still working on those, related to SSL/security • Not an issue with 201 scripts, but server • What we might find when looking at solutions • What should I do when I can’t get all green • What do I do when I can’t get all green 10/5/2018 Compsci 201, Fall 2018, Linked Lists 24