Upload
duongthuy
View
218
Download
0
Embed Size (px)
Citation preview
Sequences 10/20/2006 4:20 PM
1
Lists 1© 2004 Goodrich, Tamassia
Lists
Lists 2© 2004 Goodrich, Tamassia
Singly Linked ListA singly linked list is a concrete data structure consisting of a sequence of nodesEach node stores
elementlink to the next node
next
elem node
A B C D
∅
Lists 3© 2004 Goodrich, Tamassia
Position ADT (4th edition – 6.2.2)
The Position ADT models the notion of place within a data structure where a single object is storedIt gives a unified view of diverse ways of storing data, such as
a cell of an arraya node of a linked list
Just one method:object element(): returns the element stored at the position
Lists 4© 2004 Goodrich, Tamassia
Node List ADT (4th edition – 6.2.3)
The List ADT models a sequence of positions storing arbitrary objectsIt establishes a before/after relation between positions(for positions, think node references)Generic methods:
size(), isEmpty()
Accessor methods:first(), last()prev(p), next(p)
Update methods:replace(p, e) addBefore(p, e), addAfter(p, e),addFirst(e), addLast(e)remove(p)
Lists 5© 2004 Goodrich, Tamassia
Node List ADT error conditions
As with previous ADTs, in some situations calling a method would result in an error condition:prev(p) if p is the first positionnext(p) if p is the last positionreplace(p), remove(p), addBefore(p,e), addAfter(p,e) if p is not a valid position
Accessor methods:first(), last()prev(p), next(p)
Update methods:replace(p, e) addBefore(p, e), addAfter(p, e),addFirst(e), addLast(e)remove(p)
Lists 6© 2004 Goodrich, Tamassia
Doubly Linked ListA doubly linked list provides a natural implementation of the List ADTNodes implement Position and store:
elementlink to the previous node (new in DNode)link to the next node
Special trailer and header nodes (new)
prev next
elem
trailerheader nodes/positions
elements
node
Sequences 10/20/2006 4:20 PM
2
Lists 7© 2004 Goodrich, Tamassia
InsertionWe visualize operation addAfter(p, X), which returns position v
A B X C
A B C
p
A B C
p
X
v
p v
Lists 8© 2004 Goodrich, Tamassia
Insertion AlgorithmAlgorithm addAfter(p,e):
Create a new node vv.setElement(e)v.setPrev(p) {link v to its predecessor}v.setNext(p.getNext()) {link v to its successor}(p.getNext()).setPrev(v) {link p’s old successor to v}p.setNext(v) {link p to its new successor, v}return v {the position for the element e}
Lists 9© 2004 Goodrich, Tamassia
DeletionWe visualize remove(p), where p = last()
A B C D
p
A B C
D
p
A B CLists 10© 2004 Goodrich, Tamassia
Deletion Algorithm
Algorithm remove(p):t = p.element {a temporary variable to hold the
return value}(p.getPrev()).setNext(p.getNext()) {linking out p}(p.getNext()).setPrev(p.getPrev())p.setPrev(null) {invalidating the position p}p.setNext(null)return t
Lists 11© 2004 Goodrich, Tamassia
Full implementation
Full Java implementation of the NodePositionList in Goodrich and Tamassia 4th edition, 6.2.4
… or on http://ww0.java4.datastructures.net/source/
Lists 12© 2004 Goodrich, Tamassia
PerformanceIn the implementation of the List ADT by means of a doubly linked list
The space used by a list with n elements is O(n)The space used by each position of the list is O(1)All the operations of the List ADT run in O(1) timeOperation element() of the Position ADT runs in O(1) time
Sequences 10/20/2006 4:20 PM
3
Lists 13© 2004 Goodrich, Tamassia
Sequence ADT (just for didactic purposes…)
Lists 14© 2004 Goodrich, Tamassia
Sequence ADTThe Sequence ADT is the union of the Vector and Node List ADTsElements accessed by
Rank, orPosition
Generic methods:size(), isEmpty()
Vector-based methods:elemAtRank(r), replaceAtRank(r, o), insertAtRank(r, o), removeAtRank(r)
List-based methods:first(), last(), prev(p), next(p), replace(p, o), addBefore(p, o), addAfter(p, o), addFirst(o), addLast(o), remove(p)
Bridge methods:atRank(r), rankOf(p)
Lists 15© 2004 Goodrich, Tamassia
Linked List ImplementationA doubly linked list provides a reasonable implementation of the Sequence ADTNodes implement Position and store:
elementlink to the previous nodelink to the next node
Special trailer and header nodes
trailerheader nodes/positions
elements
Position-based methods run in constant timeRank-based methods require searching from header or trailer while keeping track of ranks; hence, run in linear time
Lists 16© 2004 Goodrich, Tamassia
Array-based ImplementationWe use a circular array storing positions A position object stores:
ElementRank
Indices f and lkeep track of first and last positions
0 1 2 3positions
elements
S
lf
Lists 17© 2004 Goodrich, Tamassia
Sequence Implementations
nninsertAtRank(i,e), removeAtRank(i,e)11addFirst(e)*, addLast(e)1naddAfter(p,e), addBefore(p,e)
n1replaceAtRank(i,e)11replace(p,e)* (next slide)
n1atRank(i), elemAtRank(i), rankOf(p)*11size(), isEmpty()
1nremove(p,e)
11first(), last(), prev(), next()
ListCircularArray
Operation
Lists 18© 2004 Goodrich, Tamassia
Some explanations
The reason rankOf(p) is O(1) in the circular array implementation is that p is a position and position object for this implementation stores the rank (index) as well as an element. So p is not a stored data element here, but a new object which corresponds to a cell in the array + its content. (slide 16 –sorry for confusion).Same for replace(p,e) – once we have p, we have the index to go and write e.addFirst(e) is O(1) in the circular array implementation – just move f (front) left.
Sequences 10/20/2006 4:20 PM
4
Lists 19© 2004 Goodrich, Tamassia
Lists in Java Collections Framework and Iterators
Lists 20© 2004 Goodrich, Tamassia
Java.util.LinkedList
Java’s LinkedList looks different from the one in the textbookMost importantly, it does not expose positions in the listIf you want to walk through the list and see/modify the elements in the list, you use an Iterator object for that list (same as with other collections which implement Iterableinterface; they all have an iterator() method which returns an Iterator)
Lists 21© 2004 Goodrich, Tamassia
Java IteratorsAn Iterator has access to elements of some Collection and can produce them in some orderCan think of it as an temporary sequence of elements with a bookmark (current) elementMethods of the Iterator<E> interface (over Collection of elements of type E):
boolean hasNext() // says whether there are any elements left to seeE next() // returns the next elementvoid remove() // removes the current element from the Collection
Lists 22© 2004 Goodrich, Tamassia
ConclusionThis is all on sequences (until we come back with sorting)Main point: you can have random access implementations of sequences, usually of fixed size, with constant time access to indices in the sequence; or linked implementations, which grow easily, but where it takes linear time to reach index i Which one is more appropriate depends on the application.