Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Introduction to Data Structures

Topic 2 – Lists
Outline

▪ The List Data Type (Chap. 4.1)


▫ Operations
▪ Random Access Memory
▪ (Dynamic) Array List (Chap. 4.1.1)
▪ Linked (Pointer Based) List (Chap. 4.1.2~4.1.5)
▪ Summary (Chap. 4.1)

34
Xingcan
ITEC2620 O
Apr 2021
The List Data Type

In computer science, a list or sequence is an abstract data type that


represents a countable number of ordered values, where the same
value may occur more than once.
-- Wikipedia

35
Xingcan
ITEC2620 O
Apr 2021
The List Data Type - Operations

▪ add(index, value) / add(value)


▪ get(index)
▪ contains(value)
▪ size()
▪ isEmpty()
▪ set(index, value)
▪ remove(value) / remove(index)

36
Xingcan
ITEC2620 O
Apr 2021
Random Access Memory
Address
100 8 bits
101 8 bits Words – 32bits/64bits
102 8 bits
103 8 bits ▪ To access some data in RAM, we
104 8 bits
105 8 bits
must know the addresses first.
106 8 bits CPU ▪ It’s fast to access some data of the
107 8 bits
108 8 bits given addresses (no seek time).
109 8 bits
110 8 bits
111 8 bits
112 ...
RAM 37
Xingcan
ITEC2620 O
Apr 2021
Array List - Storage
100 100
101 int 101 address objects
102 array[0] array[0]
1 102 2021 2021
103 103
104 104 Object
105 int 105
array[1] address array[1]
106 2 106 2222
107 107
108 2222
108 Object
109 int 109
array[2] address
110 3 110 array[2]
9999
111 111 Object 9999
112 112
fixed item size
int[3] contiguous memory location object[3]
38
Xingcan
ITEC2620 O
Apr 2021
Array List - Add and Remove
100 100
101 int 101 int
102 array[0] 102 array[0]
1 1
103 103
104 104
105 int add(1, 10) 105
array[1] int
106 106 array[1]
2 10
107 107
108 remove(1) 108
109 109 int
110 array[2] 110 array[2]
2
111 111
112 112

int[3] int[3]
39
Xingcan
ITEC2620 O
Apr 2021
Array List - Time Complexities

▪ add(index, value) / add(value) Θ(𝑛) / Θ(1) Increase the Capacity


▪ get(index) Θ(1)
▪ contains(value) Θ(𝑛)
▪ size() Θ 𝑛 ? Θ 1 ? Time/Space Trade Off
Write/Read Trade Off
▪ isEmpty() Θ 1
▪ set(index, value) Θ 1
▪ remove(value) / remove(index) Θ(𝑛) / Θ(𝑛)

40
Xingcan
ITEC2620 O
Apr 2021
Linked List - Remove and Append
100 100 100
value value value
1 1 Node1 1
Node1 Node1
address address address
2222 9999
2222 9999

2222 remove(1) 2222 add(2, 4) 2021


value value value
2 2 4
Node2 Node2 Node3
address address address
9999 9999 null

9999 9999 9999


value value value
3 3 Node2 3
Node3 Node2
address address address
null null null
2021
41
Xingcan
ITEC2620 O
Apr 2021
Linked List - Insert
100 100 100
value value value
1 1 1
Node1 Node1 Node1
address address address
9999 9999 2222

2222 2222
create value re-link value
2 2
Node2’ Node2
address address
9999 9999

9999 9999 9999


value value value
3 3 3
Node2 Node2 Node3
address address address
null null null
42
Xingcan
ITEC2620 O
Apr 2021
Linked List – Time Complexities

▪ add(index, value) / add(value) Θ(𝑛) / Θ(1)


▪ get(index) Θ(𝑛)
▪ contains(value) Θ(𝑛)
▪ size() Θ 𝑛 ? Θ 1 ?
▪ isEmpty() Θ 1
▪ set(index, value) Θ 𝑛
▪ remove(value) / remove(index) Θ(𝑛) / Θ(𝑛)

43
Xingcan
ITEC2620 O
Apr 2021
Linked List - Tips

▪ Use a dummy node before the head node.


▪ Use previous, current and next to track three successive nodes.
▪ Use doubly linked list for more flexible traversal. next next

0 1 2
Always use Array List if possible!
previous previous

Use Linked List only when you need to


remove/insert elements while iterating it .

44
Xingcan
ITEC2620 O
Apr 2021
Summary

▪ Array List: (Dynamic) Array-Based Data Structures


▪ Linked List: Pointer-Based Data Structures

45
Xingcan
ITEC2620 O
Apr 2021

You might also like