Abstract Data Types
Abstract Data Types
Reading: Chapter 1
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
V o
0 1 2 r n
V
0 1 2 r n
V
0 1 2 r n
A B C D
elements
A B C
p
A B q C
X
p q
A B X C
2019/8/30 CSCE 4133/5133: Algorithms 14
Deletion
❑ We visualize remove(p), where p = last()
p
A B C D
A B C p
A B C
2019/8/30 CSCE 4133/5133: Algorithms 15
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
…
S
0 1 2 t
…
S
0 1 2 t
2019/8/30 CSCE 4133/5133: Algorithms 20
Stack with a Singly Linked List
❑ We can implement a stack with a singly linked list
● The top element is stored at the first node of the list
● The space used is O(n) and each operation of the Stack ADT takes O(1)
time
nodes
t
elements
Algorithm isEmpty()
return (f = r)
Q
0 1 2 f r
Q
0 1 2 r f
normal configuration
Q
0 1 2 f r
wrapped-around configuration
Q
0 1 2 r f
2019/8/30 CSCE 4133/5133: Algorithms 27
Growable Array-based Queue
❑ In an push 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 push operation has amortized running time
● O(n) with the incremental strategy
● O(1) with the doubling strategy
f
elements
2019/8/30 CSCE 4133/5133: Algorithms 29