04 Lists PDF
04 Lists PDF
1
We learned…
2
Review:
Basic Data Structures (“concrete” data structures)
Array
Linked Lists
For example:
l
header trailer
3
Abstract Data Types (ADT)
Containers
Contains objects
I can INSERT
I can REMOVE
I can …..
4
Abstract Data Types seen so far
Insert = PUSH
Remove = POP Insert = ENQUEUE
Remove = DEQUEUE
STACK
QUEUE
DEQUE
Insert: InsertFirst, InsertLast
Remove: RemoveFirst RemoveLast
5
What are we going to see next ...
Generalization…
6
1st
3rd
ARRAY LIST
POSITIONAL LIST
2nd
By “index”
By “position”
(by address)
SEQUENCE
7
Lists
• Array-List ADT
• Positional-List ADT
• Sequence ADT
8
Lists or Sequences
LISTS or SEQUENCES= collection of elements in linear order
1st 2nd
SEQUENCE ADT
Combination of both
9
Array-lists
• Can access any element directly, not just first or last.
V
0 1 2 i n
10
The Array-List ADT
11
Adapter Pattern
• Two data structures A and B are often similar
• Adapt data structure B to be used as A
• Create a “wrapper class” A holding B
Examples:
– Regular array as an ArrayList, or
– ArrayList can be adapted as a Deque
Deque ArrayList
getFirst(), getLast() get(0), get(size()-1)
addFirst(e), addLast(e) add(0,e), add(size(),e)
removeFirst(), removeLast() remove(0), remove(size()-1)
12
Natural Implementation of Array-List:
with an Array
• Array V of size N
V
0 1 2 i n
13
Insertion
• In operation add(r, o), we need to make room for the
new element by shifting forward the n - r elements
V[r], …, V[n - 1]
• In the worst case (r = 0), this takes O(n) time
add(r,o):
for i = n - 1, n - 2, ... , r do V
S[i+1] ← s[i] 0 1 2 r n
S[r] ← o
n←n+1 V
0 1 2 r n
V o
0 1 2 r n
14
Deletion
V o
remove(r): 0 1 2 r n
e ← S[r]
for i = r, r + 1, ... , n - 2 do V
S[i] ← S[i + 1] 0 1 2 r n
n←n-1
return V
0 1 2 r n
15
Performance
16
Performance (contd.)
size O(1)
isEmpty O(1)
get O(1)
replace O(1)
insert O(n)
remove O(n)
17
Class java.util.ArrayList<E>
- Inherits from
• java.util.AbstractCollection<E>
• java.util.AbstractList<E>
– Implements
• Iterable<E> Implementation with
• Collection<E> extendable arrays
• List<E>
• RandomAccess
• The methods
– size(), isEmpty(), get(int) and set(int,E) in time O(1)
– add(int,E) and remove(int) in time O(n)
18
If we were to implement an array-list with a
doubly linked list it would be quite inefficient !
header trailer
get(rank) ?
19
Finding an element at a certain rank
Algorithm get(rank)
if (rank <= size()/2) { //scan forward from head
node ← header.next
for (int i=0; i < rank; i++)
node ← node.next
}else { // scan backward from the tail
node ← trailer.prev
for (int i=0; i < size()-rank-1 ; i++)
node ← node.prev
}
return node;
20
Performance with linked list …
size O(1)
isEmpty O(1)
get O(n)
replace O(n)
insert O(n)
remove O(n)
21
Positional Lists
22
first
me
previous next
23
The Positional-List ADT
24
Natural Implementation: with a Linked List
• A doubly linked list provides a natural
implementation of the Positional-List ADT
• Nodes implement Position and store: prev next
– element
– link to the previous node
– link to the next node
• Special trailer and header nodes
elem node
elements
25
Insertion
• We visualize operation addAfter(p, X), which returns position q
p
q
X
p q
26
Insertion
• We visualize operation addAfter(p, e), which returns position v
e
v
addAfter(p,e)
Create a new node v
Correct order??
v.setNext(p.getNext())
v.setElement(e)
(p.getNext()).setPrev(v)
v.setPrev(p)
p.setNext(v)
27
Insertion
• We visualize operation addAfter(p, e), which returns position v
e
v
addAfter(p,e)
Create a new node v
v.setElement(e)
v.setPrev(p)
v.setNext(p.getNext())
(p.getNext()).setPrev(v)
p.setNext(v)
28
Deletion
29
p
t
remove(p)
t ← p.element
(p.getPrev()).setNext(p.getNext())
(p.getNext()).setPrev(p.getPrev())
p.setPrev(null)
p.setNext(null)
return t
30
Performance
31
A more general ADT:
Sequence ADT
32
An array-based Implementation
2 YYC
f l
add(2,YYC)
34
0 YOW 1 YYZ 2 YVR 3 YUL
2 YYC
f l
35
2 YYC
f l
Change all other ranks
36
2 YYC
f l
3 YVR
Immediate access to
indexOf(position):
the corresponding index
position
37
Array-based Implementation
O(n)
Other methods
O(1)
38
Implementation
with Doubly Linked List
Bridges:
atIndex(i), indexOf(p): O(n)
39
Summary: Array-based implementation of
Sequences
Need to move elements
40
Summary: Implementation of Sequences by
Doubly-linked lists