Académique Documents
Professionnel Documents
Culture Documents
Structures
Stacks, Queues, & Lists
Amortized analysis
Trees
Abstract Data Types (ADTs)
Direct applications
Page-visited history in a Web browser
Undo sequence in a text editor
Chain of method calls in the Java Virtual
Machine or C++ runtime environment
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures
…
S
0 1 2 t
Elementary Data Structures 10
Performance and Limitations
Performance
Let n be the number of elements in the stack
The space used is O(n)
Each operation runs in time O(1)
Limitations
The maximum size of the stack must be defined a
priori and cannot be changed
Trying to push a new element into a full stack
causes an implementation-specific exception
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
normal configuration
Q
0 1 2 f r
wrapped-around configuration
Q
0 1 2 r f
Elementary Data Structures 22
Queue Operations
Q
0 1 2 f r
Q
0 1 2 r f
Q
0 1 2 f r
Q
0 1 2 r f
Q
0 1 2 f r
Q
0 1 2 r f
Elementary Data Structures 25
Growable Array-based Queue
In an enqueue operation, when the array is
full, instead of throwing an exception, we
can replace the array with a larger one
Similar to what we did for an array-based
stack
The enqueue operation has amortized
running time
O(n) with the incremental strategy
O(1) with the doubling strategy
V
0 1 2 r n
V
0 1 2 r n
V
0 1 2 r n
V o
0 1 2 r n
Elementary Data Structures 33
Deletion
In operation removeAtRank(r), we need to fill the
hole left by the removed element by shifting
backward the n r 1 elements V[r + 1], …, V[n 1]
In the worst case (r = 0), this takes O(n) time
V o
0 1 2 r n
V
0 1 2 r n
V
0 1 2 r n
Elementary Data Structures 34
Lists and Sequences
Outline and Reading
Singly linked list
Position ADT and List ADT (§2.2.2)
Doubly linked list (§ 2.2.2)
Sequence ADT (§ 2.2.3)
Implementations of the sequence ADT
(§ 2.2.3)
Iterators (2.2.3)
A B C D
nodes
t
elements
Elementary Data Structures 38
Queue with a Singly Linked List
We can implement a queue with a singly linked list
The front element is stored at the first node
The rear element is stored at the last node
The space used is O(n) and each operation of the
Queue ADT takes O(1) time r
nodes
f
elements
Elementary Data Structures 39
Position ADT
The Position ADT models the notion of
place within a data structure where a
single object is stored
It gives a unified view of diverse ways
of storing data, such as
a cell of an array
a node of a linked list
Just one method:
object element(): returns the element
stored at the position
elements
Elementary Data Structures 42
Insertion
We visualize operation insertAfter(p, X), which returns position q
p
A B C
p
A B q C
X
p q
A B X C
Elementary Data Structures 43
Deletion
We visualize remove(p), where p = last()
p
A B C D
A B C p
A B C
Elementary Data Structures 44
Performance
In 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) time
Operation element() of the
Position ADT runs in O(1) time
Elementary Data Structures 45
Sequence ADT
The Sequence ADT is the List-based methods:
union of the Vector and first(), last(),
List ADTs before(p), after(p),
Elements accessed by replaceElement(p, o),
Rank, or swapElements(p, q),
Position insertBefore(p, o),
insertAfter(p, o),
Generic methods: insertFirst(o),
size(), isEmpty() insertLast(o),
Vector-based methods: remove(p)
elemAtRank(r), Bridge methods:
replaceAtRank(r, o),
insertAtRank(r, o), atRank(r), rankOf(p)
removeAtRank(r)