360 likes | 367 Views
This article explores the implementation and efficiency of linked lists in Java, focusing on their use in implementing other data structures and the tradeoffs involved. It also covers the concepts of stacks and queues and provides insights into empirical and analytical analysis of data structures. Code examples and performance analysis are included.
E N D
CompSci201,Linked Lists Owen Astrachan Jeff Forbes October 19, 2017 Compsci 201, Fall 2017, Linked
N is for … • new • Allocating memory from the heap • next • Moving through elements in a list • Network • The power of Metcalfe’s Law Compsci 201, Fall 2017, Linked
Plan for the Week • APIs and Efficiency • Linear structures • Linked lists explained, uncovered, explored • How does java.util.LinkedList work from both implementation and performance perspective • How are linked lists used to implement other data structures • What are tradeoffs in using linked lists • Code: https://coursework.cs.duke.edu/201fall17/classwork-linked/ CompSci 201, Fall 2017, Linked
Stack • Last In First Out (LIFO) • Stack<String> q = new Stack<String>(); • q.push("comp "); • q.push("sci "); • q.push("is "); • q.push("great!"); • while(!q.isEmpty()) • System.out.print(q.pop()); out.print(q.pop());
Stack • Last In First Out (LIFO) • Stack<String> st= new Stack<String>(); • st.push("comp "); • st.push("sci "); • st.push("is "); • st.push("great!"); • while(!st.isEmpty()) • System.out.print(st.pop()); great! is sci comp comp sci is great!
Queue • First In First Out (FIFO) • Queue<String> q = new LinkedList<String>(); • q.add("comp "); • q.add("sci "); • q.add("is "); • q.add("great!"); • while(!q.isEmpty()) • System.out.print(q.remove()); comp sci is great! comp sci is great!
Stacks & Queues • Reasoning about stacks & queues • WOTO: http://bit.ly/201-f17-1018-1 • Useful abstraction • How to implement? CompSci 201, Fall 2017, Linked
Empirical and Analytical Analysis • We can run programs to look at "efficiency" • Depends on machine, environment, programs • We can analyze mathematically to look at efficiency from a different point of view • Depends on being able to employ mathematics • What works in theory may not … • We will work on doing both, leading to a better understanding in many dimensions
What is a java.util.List in Java? • Collection of elements, operations? • Add, remove, traverse, … • 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?
What's the Difference Here? • How does find-a-track work? Fast forward?
Analyze Data Structures publicdoubleremoveFirst(List<String> list) { double start = System.nanoTime(); while (list.size() != 1){ list.remove(0); } double end = System.nanoTime(); return (end-start)/1e9; } List<String> linked = newLinkedList<String>(); List<String> array = newArrayList<String>(); doubleltime = splicer.removeFirst(splicer.create(linked,100000)); doubleatime = splicer.removeFirst(splicer.create(array,100000)); • Time taken to remove the first element? https://coursework.cs.duke.edu/ola/201-linked-spring17/blob/master/src/ListSplicer.java
Remove First • Why are timings good? • Why are timings bad?
Remove Middle Index publicdoubleremoveMiddleIndex(List<String> list) { double start = System.nanoTime(); while (list.size() != 1){ list.remove(list.size()/2); } double end = System.nanoTime(); return (end-start)/1e9; } • What operations could be expensive here? • Explicit: size, remove (only one is expensive) • Implicit: find nth element (may be very costly)
ArrayList and LinkedList as ADTs • As an ADT (abstract data type) ArrayList supports • Constant-time or O(1) access to the k-th element • Amortized linear or O(n) storage/time with add • Total storage used in n-element vector is approx. 2n, spread over all accesses/additions (why?) • Add/remove in middle is "expensive" O(n), why? • What's underneath here? How Implemented? • Concrete: array – contiguous memory, must be contiguous to support random access • Element 20 = beginning + 20 x size of a pointer
ArrayList and LinkedList as ADTs • LinkedList as ADT • Constant-time or O(1) insertion/deletion anywhere, but… • Linear or O(n) time to find where, sequential search • Linked good for add/remove at front • Splicing into middle, also for 'sparse' structures • What's underneath? How Implemented • Low-level linked lists, self-referential structures • More memory intensive than array: two pointers
Remove Middle in Pictures for(int k=middle; … a[k] = alist[k+1] • Find middle element: happens instantly or O(1) • alist(location) + n/2 * sizeof(pointer) since ArrayList holds pointers • Shifting requires moving n/2 pointers, but they are all contiguous in memory: cache performance ArrayList<> alist
Remove Middle in Pictures • Find middle element: have to follow pointers between elements • Follow n/2 pointers, but all over memory, so takes time to move from memory->cache->use • Removing middle: instantaneous, no shifting, just re-assign a couple of pointers (back pointers too) • Blue points to Yellow Linked<> llist
Analytical Analysis • Since LinkedList is roughly linear • Time to remove first element is constant, but must be done N times • Time for one removal is O(1) --- constant and doesn't depend on N • Time for all removals is O(N) – linear in N, but slope doesn't matter • For ArrayList, removing first element entails … • Shifting N-1 elements, so this is O(N) • All: (N-1) + (N-2) + … + 3 + 2 + 1 = O(?)
Barbara Liskov Turing Award Winner in 2008 for For contributions to practical and theoretical foundations of programming language and system design, especially related to data abstraction, fault tolerance, and distributed computing. The advice I give people in general is that you should figure out what you like to do, and what you can do well—and the two are not all that dissimilar, because you don’t typically like doing something if you don’t do it well. … So you should instead watch—be aware of what you’re doing, and what the opportunities are, and step into what seems right, and see where it takes you.
Pointers, References, Structures • Study LinkedList and linked lists from basics • Useful in understanding Java implementations • Useful to understand C, C++ • Useful in understanding trees • Required in other courses, interviews, etc. • Low-level abstraction, high-order abstraction • How can you implement structures that allow arbitrary splicing in the middle?
Goldilocks and the Hashtable • A hashtable is a collection of buckets • Find the right bucket and search it • Bucket organization? • Array, linked list, search tree
Structuring Data: The inside story • How does a HashSet work? SimpleHashStringSet, almost the same as HashMap • What happens with add(key) in a HashSet? • What happens with contains(key)? • What happens with remove(key)? • In diagram below, what's in each cell of myTable? • ArrayList: advantages? Disadvantages?
Set Implementations SetDriver.java • Array: search entire array for each add • Class java.util.HashSet • HashArray: buckets are ArrayList objects • HashLink: buckets are low-level linked lists https://coursework.cs.duke.edu/201fall17/classwork-linked/
Set Implementations, SetStress.java • Can we run without edit/recompile/run cycle? • Benefits? Drawbacks? • Why Java interfaces are a good idea for allowing different concrete implementations https://coursework.cs.duke.edu/201fall17/classwork-linked/
p Linked lists, CDT and ADT • As an ADT • A list is empty, or contains an element and a list • ( ) or (x, (y, ( ) ) ) • As a picture • CDT (concrete data type) pojo: plain old Java object public class Node{ Node p = new Node(); String info; p.info= "hello"; Node next; p.next= null; }
Building linked lists • Add words to the front of a list (draw a picture) • Create new node pointing to list, reset start of list public class Node { String info; Node next; public Node(String s, Node link){ value = s; next = link; } } // … declarations here ListNode list = null; while (scanner.hasNext()) { list = new ListNode(scanner.next(), list); } • What about adding to the end of the list? • http://www.pythontutor.com/java.html
Adding to End (iterative) voidaddAtEnd(Node head, String value) { if(head == null) head= new Node(value, null); else{ Node current = head; while(current.next!= null) current = current.next; // what’s true here? current.next= new Node(value, null); } } • What is true after the while loop? • How can we do this recursively?
Adding to End (recursive) publicNode addAtEnd(Node head, String value) { if(head == null) returnnew Node(value, null); head.next= addAtEnd(head.next, value); returnhead; } • What is the base case?
list list A B Dissection of add-to-front • List initially empty • First node has first word • Each new word causes new node to be created • New node added to front list = new Node(word,list); Node(String s, Node link) { info = s; next = link;} • rhs of operator = completely evaluated before assignment
Standard list processing (iterative) • Visit all nodes once, e.g., count them or process them public intsize(Node list){ int count = 0; while (list != null) { count += 1; list = list.next; } return count; } • Should we be concerned that list is null on return? • Can we change the data in the list nodes? • Append “s” to all strings in list?
Removing Node from list, "cat" list public Node remove(Node list, String s){ Node start = list; // special case 'cat' first, not shown while (list.next != null) { if (list.next.info.equals(s)) { list.next = list.next.next; break; } list = list.next; } return start; } "dog" "cat" "pig"
Linked List idioms • Sometimes check list == null and list.next == null • Short-circuit evaluation in how to do this? • First node can be tricky to process, e.g., remove • Has no node before it. • Solution: put a "header" or "empty" node first • Typically loop: while(list != null) • Can be useful to do while (list.next != null) • Must be sure list != null in writing this!!!
Link Questions http://bit.ly/201-f17-1018-2 Why is the parameter in remove method Object and not String?