Upload
arleen-parks
View
217
Download
0
Embed Size (px)
Citation preview
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.
LECTURE 29:LINKED LIST-BASED LIST
CSC 212 – Data Structures
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
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
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
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
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
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
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
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!
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!
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?
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
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
Key Concept For Rest of Week
Node in linked list
equivalent to index in array
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()
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
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)
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)
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)
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
Does Absolute Position Matter?
Relativity Can Be Important
Your Turn
Get into your groups and complete activity
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