1 tcss 342, winter 2005 lecture notes linked list implementation weiss ch. 6, pp. 183-205 weiss ch....

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

Upload: nathaniel-wright

Post on 19-Jan-2016

220 views

Category:

Documents


1 download

TRANSCRIPT

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

1

TCSS 342, Winter 2005 Lecture Notes

Linked List Implementation

Weiss Ch. 6, pp. 183-205Weiss Ch. 17, pp. 537-548

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

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

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

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.

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

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.

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

5

An example collection: List

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

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

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

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

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.

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

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

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

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?

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

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.*;

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

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]

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

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);

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

13

Analysis of ArrayList runtime

OPERATIONadd to start of listadd to end of listadd at given indexcleargetfind index of an objectremove first elementremove last elementremove at given indexsetsizetoString

RUNTIME (Big-Oh) O(n) O(1) O(n) O(1) O(1) O(n) O(n) O(1) O(n) O(1) O(1) O(n)

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

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?

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

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

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

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?

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

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

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

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; }}

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

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:

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

20

Linked node problems (b)

2. before:

after:

3. before:

after:

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

21

Linked node problems (c)

4. before:

after:

5. before:

after:

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

22

Linked node problems (d)

6. before:

after:

7. before:

after:

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

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)

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

24

Linked list implementation

/* Models an entire linked list. */public class MyLinkedList { private Node myFront;

public MyLinkedList() { myFront = null; }

/* Methods go here */}

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

25

Some list states of interest

empty list(myFront == null)

list with one element

list with manyelements

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

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

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

27

Analysis of LinkedList runtime

OPERATIONadd to start of listadd to end of listadd at given indexcleargetfind index of an objectremove first elementremove last elementremove at given indexsetsizetoString

RUNTIME (Big-Oh) O(1) O(n) O(n) O(1) O(n) O(n) O(1) O(n) O(n) O(n) O(n) O(n)

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

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?

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

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

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

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?

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

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

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

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

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

33

Improved LinkedList runtime

OPERATIONadd to start of listadd to end of listadd at given indexcleargetfind index of an objectremove first elementremove last elementremove at given indexsetsizetoString

RUNTIME (Big-Oh) O(1) O(1) O(n) O(1) O(n) O(n) O(1) O(1) O(n) O(n) O(1) O(n)

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

34

A particularly slow idiom// print every element of linked listfor (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(n2), which is much worse than

O(n) this code will take prohibitively long to run for

large data sizes

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

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"

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

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 objectin this collection, thenadvances the iterator;throws an exception if no more elements remain

public void remove()Deletes the element that waslast returned by next (not always supported)

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

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?

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

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)

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

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?)

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

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

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

41

Fixing the slow LL idiom

// print every element of the listfor (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);}

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

42

Iterator usage exampleMyLinkedList names = new MyLinkedList();// ... fill the list with some data ...

// print every name in the list, in upper caseIterator 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 }

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

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

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

44

Iterator is still not perfect// print odd-valued elements, with their indexesIterator 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?

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

45

More iterator problems// add a 0 after any odd elementIterator 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!

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

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)

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

47

ListIterator usage// print odd-valued elements, with their indexesfor (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 elementfor (ListIterator itr = list.listIterator(); itr.hasNext(); ) { int element = ((Integer)itr.next()).intValue(); if (element % 2 == 1) itr.add(0);}

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

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?

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

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