1 TCSS 342, Winter 2005 Lecture Notes Linked List Implementation Weiss Ch. 6, pp. 183-205 Weiss Ch. 17, pp. 537-548.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
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.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Collections Sets - no duplicates Lists - duplicates allowed Maps - key / value pairs A collection is an Object which contains other Objects. There are.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
15-Jun-15 Lists in Java Part of the Collections Framework.
Algorithm Programming Containers in Java Bar-Ilan University תשס " ו by Moshe Fresko.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Vectors. Vectors and arrays A Vector is like an array of Object s Differences between arrays and Vector s: –Arrays have special syntax; Vector s don’t.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
TCSS 342, Winter 2005 Lecture Notes
12-Jul-15 Lists in Java Part of the Collections Framework.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 11: Java Collections Framework.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Stacks, Queues, and Deques
CSE 143 Lecture 15 Sets and Maps; Iterators reading: ; 13.2; 15.3; 16.5 slides adapted from Marty Stepp
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
1 Building Java Programs Java Collections Framework.
LinkedList Many slides from Horstmann modified by Dr V.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
1.0tCopyright © 1998 Purple Technology, Inc. 1 Java Collections Framework Authored by Alex Chaffee Copyright © 1998 Purple Technology, Inc. All rights.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
CS 106 Introduction to Computer Science I 04 / 25 / 2008 Instructor: Michael Eckmann.
Java 2 Collections Bartosz Walter Software Engineering II.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
CSE 143 Lecture 2 ArrayList reading: 10.1 slides created by Marty Stepp
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
List Interface and Linked List Mrs. Furman March 25, 2010.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
CSE 143 Lecture 12: Sets and Maps reading:
Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode.
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
CSE 143 Lecture 16 Iterators; Grammars reading: 11.1, 15.3, 16.5 slides created by Marty Stepp
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
LINKED LISTS.
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs)
Chapter 5: The List Abstract Data Type
CSE 373 Implementing a Stack/Queue as a Linked List
The List ADT Reading: Textbook Sections 3.1 – 3.5
Linked Lists Chapter 5 (continued)
Data Structures Lakshmish Ramaswamy.
Programming Abstractions
Top Ten Words that Almost Rhyme with “Peas”
TCSS 143, Autumn 2004 Lecture Notes
Programming in Java Lecture 11: ArrayList
The List ADT Reading: Textbook Sections 3.1 – 3.5
CSE 143 Lecture 27: Advanced List Implementation
slides adapted from Marty Stepp
Programming Abstractions
Computer Science and Engineering
ArrayLists 22-Feb-19.
Linked Lists Chapter 5 (continued)
CSC 143 Java Linked Lists.
TCSS 143, Autumn 2004 Lecture Notes
Part of the Collections Framework
CSE 143 Lecture 21 Advanced List Implementation
Linked Lists Chapter 5 (continued)
Java Generics & Iterators
Presentation transcript:

1 TCSS 342, Winter 2005 Lecture Notes Linked List Implementation Weiss Ch. 6, pp Weiss Ch. 17, pp

2 Collections collection: an object that stores data inside it; a.k.a. a "data structure" the objects stored are called elements some maintain an ordering, some don't some collections allow duplicates, some don't an array is like a very crude "collection" typical operations: add element, remove element, clear all elements, contains or find element, get size most collections are built with particular kinds of data, or particular operations on that data, in mind examples: java.util.ArrayList, java.util.HashMap, java.util.TreeSet

3 Java's Collection interface Java provides an interface java.util.Collection to represent many kinds of collections. It has the following methods: public boolean add(Object o) Appends the specified element to this collection. public void clear() Removes all of the elements of this collection. public boolean contains(Object o) Returns true if this collection contains the specified element. public boolean containsAll(Collection coll) Returns true if this collection contains all of the elements in the specified collection.

4 Collection interface,cont'd. public boolean isEmpty() Returns true if this list contains no elements. public Iterator iterator() Returns a special object for examining the elements of the list in order (seen later). public boolean remove(Object o) Removes the first occurrence in this list of the specified element. public int size() Returns the number of elements in this list. public Object[] toArray() Returns an array containing all of the elements from this list.

5 An example collection: List list: an ordered sequence of elements, each accessible by a 0-based index one of the most basic collections

6 List features ORDERING: maintains order elements were added (new elements are added to the end by default) DUPLICATES: yes (allowed) OPERATIONS: add element to end of list, insert element at given index, clear all elements, search for element, get element at given index, remove element at given index, get size some of these operations are inefficient! (seen later) list manages its own size; user of the list does not need to worry about overfilling it

7 Java's List interface Java also has an interface java.util.List to represent a list of objects. It adds the following methods to those in Collection : (a partial list) public void add(int index, Object o) Inserts the specified element at the specified position in this list. public Object get(int index) Returns the element at the specified position in this list. public int indexOf(Object o) Returns the index in this list of the first occurrence of the specified element, or -1 if the list does not contain it.

8 List interface, cont'd. public int lastIndexOf(Object o) Returns the index in this list of the last occurrence of the specified element, or -1 if the list does not contain it. public Object remove(int index) Removes the object at the specified position in this list. public Object set(int index, Object o) Replaces the element at the specified position in this list with the specified element. Notice that the methods added to Collection by List all deal with indexes; a list has indexes while a general collection may not

9 Some list questions all of the list operations on the previous slide could be performed using an array instead! open question: What are some reasons why we might want to use a list class, rather than an array, to store our data? thought question: How might a List be implemented, under the hood? why do all the List methods use type Object ?

10 Array list array list: a list implemented using an array to store the elements encapsulates array and # of elements (size) in Java: java.util.ArrayList when you want to use ArrayList, remember to import java.util.*;

11 ArrayList features think of it as an auto-resizing array that can hold any type of object, with many convenient methods maintains most of the benefits of arrays, such as fast random access frees us from some tedious operations on arrays, such as sliding elements and resizing can call toString on an ArrayList to print its elements [1, 2.65, Marty Stepp, Hello]

12 Collections class Class java.util.Collections has many utility methods that do useful things to collections public static void copy(List dest, List src) public static void fill(List list, Object value) public static Object max(Collection coll) public static Object min(Collection coll) public static void reverse(List list) public static void shuffle(List list) public static void sort(List list) public static void swap(List list, int i, int j) Example: Collections.sort(myArrayList);

13 Analysis of ArrayList runtime OPERATION add to start of list add to end of list add at given index clear get find index of an object remove first element remove last element remove at given index set size toString RUNTIME (Big-Oh) O(n) O(1) O(n) O(1) O(n) O(1) O(n) O(1) O(n)

14 Open questions Based on the preceding analysis, when is an ArrayList a good collection to use? When is it a poor performer? Is there a way that we could fix some of the problems with the ArrayList ? Should we represent our list in a different way?

15 some ArrayList problems an insertion into a full list causes a large reallocation an insertion to the front "slides" down the subsequent items (slow!) a removal also "slides" down subsequent items still need to use indexes/subscripts a lot somewhat ugly syntax to use it

16 The underlying issue the elements of an ArrayList are too tightly attached; can't easily rearrange them can we break the element storage apart into a more dynamic and flexible structure?

17 Nodes: objects to store elements let's make a special "node" type of object that represents a storage slot to hold one element of a list each node will keep a reference to the node after it (the "next" node) the last node will have next == null (drawn as / ), signifying the end of the list

18 Node implementation /* Stores one element of a linked list. */ public class Node { public Object element; public Node next; public Node(Object element) { this(element, null); } public Node(Object element, Node next) { this.element = element; this.next = next; }

19 Linked node problems (a) Let's examine sample chains of nodes together, and try to write the correct code for each each Node stores an Integer object 1. before: after:

20 Linked node problems (b) 2. before: after: 3. before: after:

21 Linked node problems (c) 4. before: after: 5. before: after:

22 Linked node problems (d) 6. before: after: 7. before: after:

23 Linked list linked list: a list implemented using a linked sequence of nodes the list only needs to keep a reference to the first node (we might name it myFront ) in Java: java.util.LinkedList (but we'll write our own)

24 Linked list implementation /* Models an entire linked list. */ public class MyLinkedList { private Node myFront; public MyLinkedList() { myFront = null; } /* Methods go here */ }

25 Some list states of interest empty list ( myFront == null ) list with one element list with many elements

26 Let's draw them together... an add operation at the front, back, and middle a remove operation a get operation a set operation an index of (searching) operation

27 Analysis of LinkedList runtime OPERATION add to start of list add to end of list add at given index clear get find index of an object remove first element remove last element remove at given index set size toString RUNTIME (Big-Oh) O(1) O(n) O(1) O(n) O(1) O(n)

28 An optimization: mySize problem: array list has a O(1) size method, but the linked list needs O(n) time solution: add a mySize field to our linked list what changes must be made to the implementation of the methods of the linked list?

29 A variation: dummy header dummy header: a front node intentionally left blank myFront always refers to dummy header ( myFront will never be null ) requires minor modification to many methods surprisingly, makes implementation much easier

30 An optimization: myBack problem: array list has O(1) get/remove of last element, but the linked list needs O(n) solution: add a myBack pointer to the last node which methods' Big-Oh runtime improve to O(1)? what complications does this add to the implementation of the methods of the list?

31 Doubly-linked lists add a prev pointer to our Node class allows backward iteration (for ListIterator ) some methods need to be modified when adding or removing a node, we must fix the prev and next pointers to have the correct value! can make it easier to implement some methods such as remove

32 Combining the approaches Most actual linked list implementations are doubly-linked and use a dummy header and dummy tail this actually makes a very clean implementation for all linked list methods and provides good efficiency for as many operations as possible

33 Improved LinkedList runtime OPERATION add to start of list add to end of list add at given index clear get find index of an object remove first element remove last element remove at given index set size toString RUNTIME (Big-Oh) O(1) O(n) O(1) O(n) O(1) O(n) O(1) O(n)

34 A particularly slow idiom // print every element of linked list for (int i = 0; i < list.size(); i++) { Object element = list.get(i); System.out.println(i + ": " + element); } This code executes an O(n) operation ( get ) every time through a loop that runs n times! Its runtime is O(n 2 ), which is much worse than O(n) this code will take prohibitively long to run for large data sizes

35 The problem of position The code on the previous slide is wasteful because it throws away the position each time every call to get has to re-traverse the list! it would be much better if we could somehow keep the list in place at each index as we looped through it Java uses special objects to represent a position of a collection as it's being examined... these objects are called "iterators"

36 Iterators in Java interface java.util.Iterator public boolean hasNext() Returns true if there are more elements to see public Object next() Returns the next object in this collection, then advances the iterator; throws an exception if no more elements remain public void remove() Deletes the element that was last returned by next (not always supported)

37 Iterators on array lists An iterator on an array list is simple; it maintains an index of its current node iterators are not as useful on array lists Why? What is the Big-Oh of get and set? What is the Big-Oh of add and remove? Does the array list iterator improve this?

38 Iterators on linked lists an iterator on a linked list maintains (at least) its current index and a reference to that node when iterator() is called on a linked list, the iterator initially refers to the first node (index 0)

39 Linked list iterator iteration when next() is called, the iterator: grabs the current myNode 's element value (32) follows the next pointer on its node and increments its index returns the element it grabbed (32) hasNext is determined by whether myNode is null (Why?)

40 How does remove work? remove is supposed to remove the last value that was returned by next to do this, we need to delete the node before myNode, which may require modification

41 Fixing the slow LL idiom // print every element of the list for (int i = 0; i < list.size(); i++) { Object element = list.get(i); System.out.println(i + ": " + element); } Iterator itr = list.iterator(); for (int i = 0; itr.hasNext(); i++) { Object element = itr.next(); System.out.println(i + ": " + element); }

42 Iterator usage example MyLinkedList names = new MyLinkedList(); //... fill the list with some data... // print every name in the list, in upper case Iterator itr = myList.iterator(); while (itr.hasNext()) { String element = (String)itr.next(); System.out.println(element.toUpperCase()); } // remove strings from list that start with "m" itr = myList.iterator(); while (itr.hasNext()) { String element = (String)itr.next(); if (element.startsWith("m")) itr.remove(); // remove element we just saw }

43 Benefits of iterators speed up loops over linked lists' elements What is the Big-Oh of each iterator method? provide a unified way to examine all elements of a collection every collection in Java has an iterator method in fact, that's the only guaranteed way to examine the elements of any Collection (see Slide 4) don't need to look up different collections' method names to see how to examine their elements don't have to use indexes as much on lists

44 Iterator is still not perfect // print odd-valued elements, with their indexes Iterator itr = list.iterator(); for (int i = 0; itr.hasNext(); i++) { int element = ((Integer)itr.next()).intValue(); if (element % 2 == 1) System.out.println(i + ": " + element); i++; } We still had to maintain the index variable i so that we could print the index of each element Wouldn't it be nice if the iterator could just tell us the index?

45 More iterator problems // add a 0 after any odd element Iterator itr = list.iterator(); int i = 0; while (itr.hasNext()) { int element = ((Integer)itr.next()).intValue(); if (element % 2 == 1) list.add(i, new Integer(0)); // fails } can't use the iterator to add, set element values (iterator is programmed to crash if list is modified) the iterator speeds up get and remove loops only the iterator really should be able to help us speed up loops that add elements or set elements' values!

46 ListIterator interface Java interface java.util.ListIterator extends Iterator to add these methods: public void add(Object o) public int nextIndex() public int previousIndex() public void set(Object o) ListIterator is also able to iterate in reverse: public boolean hasPrevious() public Object previous() get a ListIterator by calling these methods on the List : (both ArrayList and LinkedList have them) public ListIterator listIterator() public ListIterator listIterator(int index)

47 ListIterator usage // print odd-valued elements, with their indexes for (ListIterator itr = list.listIterator(); itr.hasNext(); ) { int element = ((Integer)itr.next()).intValue(); if (element % 2 == 1) System.out.println(itr.previousIndex() + ": " + element); } // add a 0 after any odd element for (ListIterator itr = list.listIterator(); itr.hasNext(); ) { int element = ((Integer)itr.next()).intValue(); if (element % 2 == 1) itr.add(0); }

48 ListIterator benefits using the ListIterator internally to implement the methods of the linked list can make the code cleaner and simpler: public Object get(int index) { ListIterator itr = listIterator(index); return itr.next(); } should the code check the index for validity?

49 Summary lists are ordered, integer-indexed collections that allow duplicates lists are good for storing elements in order of insertion and traversing them in that order linked lists are faster for add / remove operations at the front and back, but slower than array lists for arbitrary get / set operations lists are bad for searching for elements and for lots of arbitrary add / remove operations