26
Problem of the Day What do you get when you cross a mountain climber and a grape?

Problem of the Day What do you get when you cross a mountain climber and a grape?

Embed Size (px)

Citation preview

Page 1: Problem of the Day  What do you get when you cross a mountain climber and a grape?

Problem of the Day

What do you get when you cross a mountain climber and a grape?

Page 2: Problem of the Day  What do you get when you cross a mountain climber and a grape?

Problem of the Day

What do you get when you cross a mountain climber and a grape?

Nothing, you cannot cross a scalar.

Page 3: Problem of the Day  What do you get when you cross a mountain climber and a grape?

LECTURE 29:LINKED LIST-BASED LIST

CSC 212 – Data Structures

Page 4: Problem of the Day  What do you get when you cross a mountain climber and a grape?

public interface List<E> extends Collection {public E removeFirst()

throws EmptyCollectionException;public E removeLast()

throws EmptyCollectionException;public E remove(E elem)

throws ElementNotFoundException;

public E first() throws EmptyCollectionException;public E last() throws EmptyCollectionException;

public boolean contains(E elem);

public get(int i) throws EmptyCollectionException;

}

List Interface

Page 5: Problem of the Day  What do you get when you cross a mountain climber and a grape?

public interface OrderedList<E> extends List<E> {public void add(E elem);

}

public interface UnorderedList<E> extends List<E> {public void addToFront(E elem);public void addToRear(E elem);public void addAfter(E elem, E tgt)

throws ElementNotFoundException;

}

List Subinterfaces

Page 6: Problem of the Day  What do you get when you cross a mountain climber and a grape?

Array-based List.addAt()

addAt(i, e) “shifts” elements to make space Needs to make hole in array to place element

Can take O(n) time for this shifting When adding list’s end may take O(1) time,

but… Constant time amortizes cost of growing

array0 1 2

e

n-1i

Page 7: Problem of the Day  What do you get when you cross a mountain climber and a grape?

Array-based List.remove()

remove(e) “shifts” elements down to fill hole Not only way, but other options are difficult

& slow O(n) time required in the general case

But for specific situations could take only O(1) time

But must consider worst case when computing big-Oh0 1 2

e

n-1i

Page 8: Problem of the Day  What do you get when you cross a mountain climber and a grape?

Something About LIST’s Methods contains() checks if element already

included Requires comparing with elements currently

in LIST remove() removes element IF in LIST

already Requires comparing with elements currently

in LIST add() keeps order by placing element

correctlyOR

addAfter() places element after finding target Requires comparing with elements currently

in LIST

Page 9: Problem of the Day  What do you get when you cross a mountain climber and a grape?

What They Have In Common

All methods first search through the LIST Searching differs if LIST ordered or

unordered Each method has different action with

found element Could rewrite code, but want to be

VERY lazy Use private method that returns index

where found All (public) methods then rely upon this

private one

Page 10: Problem of the Day  What do you get when you cross a mountain climber and a grape?

What They Have In Common

All methods first search through the LIST Searching differs if LIST ordered or

unordered Each method has different action with

found element Could rewrite code, but want to be

VERY lazy Use private method that returns index

where found All (public) methods then rely upon this

private one

Page 11: Problem of the Day  What do you get when you cross a mountain climber and a grape?

What They Have In Common

All methods first search through the LIST Searching differs if LIST ordered or

unordered Each method has different action with

found element Could rewrite code, but want to be

VERY lazy Use private method that returns index

where found All (public) methods then rely upon this

private one

WAIT!

Page 12: Problem of the Day  What do you get when you cross a mountain climber and a grape?

What They Have In Common

All methods first search through the LIST Searching differs if LIST ordered or

unordered Each method has different action with

found element Could rewrite code, but want to be

VERY lazy Use private method that returns index

where found All (public) methods then rely upon this

private one

WAIT!

Page 13: Problem of the Day  What do you get when you cross a mountain climber and a grape?

What Does find() Do?

find() returns location element found Array organized via indices, so returning int logical

Linked list has no indices & not centrally organized

Must we write separate find() methods?

Page 14: Problem of the Day  What do you get when you cross a mountain climber and a grape?

What Does find() Do?

find() returns location element found Array organized via indices, so returning int logical

Linked list has no indices & not centrally organized

Must we write separate find() methods? The different LIST implementations not

related All of the code rewritten between types of

LISTs This duplication is not & should not be a

surprise

Page 15: Problem of the Day  What do you get when you cross a mountain climber and a grape?

What Does find() Do?

find() returns location element found Array organized via indices, so returning int logical

Linked list has no indices & not centrally organized

Nodes organize data held within a linked list In linked list-based LIST, find() returns Node Can move forward & backward if doubly-

linked If element not in LIST, method can return null

Bigger generalization from this discussion Node in linked list equivalent to index in

array

Page 16: Problem of the Day  What do you get when you cross a mountain climber and a grape?

Key Concept For Rest of Week

Node in linked list

equivalent to index in array

Page 17: Problem of the Day  What do you get when you cross a mountain climber and a grape?

What About add*() Methods?

Work similar among these methods, too All of these methods must create new Node Update size field in each of these Would still like to avoid having to duplicate

code Difference is WHERE node will be needed

addFirst() & addRear() work at LIST’s ends Middle of LIST used (usually) by add() & addAfter()

Page 18: Problem of the Day  What do you get when you cross a mountain climber and a grape?

What About add*() Methods?

Work similar among these methods, too All of these methods must create new Node Update size field in each of these Would still like to avoid having to duplicate

code Difference is WHERE node will be needed

addFirst() & addRear() work at LIST’s ends Middle of LIST used (usually) by add() & addAfter()

Must write each; little overlapping code here prev & next set differently; bulk of code is

there

Page 19: Problem of the Day  What do you get when you cross a mountain climber and a grape?

ArrayList v. LinkedListMethod ArrayList LinkedList

removeFirst() O(n) O(1)

removeLast() O(1) O(1)

addToFront() O(n) O(1)

addToRear() O(1) (amortized) O(1)

addAfter() O(n) O(n)

first(), last() O(1) O(1)

add(), contains(),remove()

O(n) O(n)

Page 20: Problem of the Day  What do you get when you cross a mountain climber and a grape?

ArrayList v. LinkedListMethod ArrayList LinkedList

removeFirst() O(n) O(1)

removeLast() O(1) O(1)

addToFront() O(n) O(1)

addToRear() O(1) (amortized) O(1)

addAfter() O(n) O(n)

first(), last() O(1) O(1)

add(), contains(),remove()

O(n) O(n)

Page 21: Problem of the Day  What do you get when you cross a mountain climber and a grape?

ArrayList v. LinkedListMethod ArrayList LinkedList

removeFirst() O(n) O(1)

removeLast() O(1) O(1)

addToFront() O(n) O(1)

addToRear() O(1) (amortized) O(1)

addAfter() O(n) O(n)

first(), last() O(1) O(1)

add(), contains(),remove()

O(n) O(n)

get(),addAt() O(1) O(n)

Page 22: Problem of the Day  What do you get when you cross a mountain climber and a grape?

Collection which we can access all elements Add element after an existing one Collection can remove any element it

contains Loop over all elements without removing

them LIST ADTs differ in how they provide

access ARRAYLIST’s indices give quick access to

specific position Good at working at relative location with LinkedList

LIST ADT

Page 23: Problem of the Day  What do you get when you cross a mountain climber and a grape?

Does Absolute Position Matter?

Page 24: Problem of the Day  What do you get when you cross a mountain climber and a grape?

Relativity Can Be Important

Page 25: Problem of the Day  What do you get when you cross a mountain climber and a grape?

Your Turn

Get into your groups and complete activity

Page 26: Problem of the Day  What do you get when you cross a mountain climber and a grape?

For Next Lecture

Read section 7.1 – 7.2 for Wednesday’s lecture What is an Iterator and what does it do? How do we connect ideas of Iterable & LIST? What are for-each loops & why do they make

life easy?

Week #10 assignment posted to Angel As usual, will be due tomorrow