01 1 Arrays and Lists
01 1 Arrays and Lists
Neil Rhodes
Department of Computer Science and Engineering
University of California, San Diego
1 Arrays
2 Linked Lists
w long[5];
long arr[] = ne
long arr[5];
5
arr = [None] *
1 5 17 3 25
1 5 17 3 25
8 2 36 5 3
Definition
Array:
Contiguous area of memory
Definition
Array:
Contiguous area of memory consisting of
equal-size elements
Definition
Array:
Contiguous area of memory consisting of
equal-size elements indexed by contiguous
integers.
1 2 3 4 5 6 7
What’s Special About Arrays?
1 2 3 4 5 6 7
What’s Special About Arrays?
Constant-time access
1 2 3 4 5 6 7
What’s Special About Arrays?
Constant-time access
array_addr
1 2 3 4 5 6 7
What’s Special About Arrays?
Constant-time access
array_addr + elem_size × ( )
1 2 3 4 5 6 7
What’s Special About Arrays?
Constant-time access
array_addr + elem_size × (i − first_index)
1 2 3 4 5 6 7
Multi-Dimensional Arrays
Multi-Dimensional Arrays
(1, 1)
Multi-Dimensional Arrays
(3,4)
Multi-Dimensional Arrays
(3,4)
(3 − 1) × 6
Multi-Dimensional Arrays
(3,4)
(3 − 1) × 6 + (4 − 1)
Multi-Dimensional Arrays
(3,4)
(3,4)
array_addr +
elem_size × ((3 − 1) × 6 + (4 − 1))
(1, 1)
(1, 2)
(1, 3)
(1, 4)
(1, 5)
(1, 6)
(2, 1)
...
(1, 1)
(1, 2)
(1, 3)
(1, 4)
Row-major
(1, 5)
(1, 6)
(2, 1)
...
(1, 1) (1, 1)
(1, 2) (2, 1)
(1, 3) (3, 1)
(1, 4) (1, 2)
Row-major
(1, 5) (2, 2)
(1, 6) (3, 2)
(2, 1) (1, 3)
... ...
(1, 1) (1, 1)
(1, 2) (2, 1)
(1, 3) (3, 1)
(1, 4) (1, 2)
Row-major Column-major
(1, 5) (2, 2)
(1, 6) (3, 2)
(2, 1) (1, 3)
... ...
Times for Common Operations
Add Remove
Beginning
End
Middle
Times for Common Operations
Add Remove
Beginning
End
Middle
5 8 3 12
Times for Common Operations
Add Remove
Beginning
End O(1)
Middle
5 8 3 12 4
Times for Common Operations
Add Remove
Beginning
End O(1)
Middle
5 8 3 12 4
Times for Common Operations
Add Remove
Beginning
End O(1) O(1)
Middle
5 8 3 12
Times for Common Operations
Add Remove
Beginning O(n)
End O(1) O(1)
Middle
8 3 12
Times for Common Operations
Add Remove
Beginning O(n)
End O(1) O(1)
Middle
8 3 12
Times for Common Operations
Add Remove
Beginning O(n)
End O(1) O(1)
Middle
8 3 12
Times for Common Operations
Add Remove
Beginning O(n)
End O(1) O(1)
Middle
8 3 12
Times for Common Operations
Add Remove
Beginning O(n) O(n)
End O(1) O(1)
Middle
8 3 12
Times for Common Operations
Add Remove
Beginning O(n) O(n)
End O(1) O(1)
Middle O(n) O(n)
8 3 12
Summary
Summary
Array: contiguous area of memory
consisting of equal-size elements
indexed by contiguous integers.
Summary
Array: contiguous area of memory
consisting of equal-size elements
indexed by contiguous integers.
Constant-time access to any element.
Summary
Array: contiguous area of memory
consisting of equal-size elements
indexed by contiguous integers.
Constant-time access to any element.
Constant time to add/remove at the
end.
Summary
Array: contiguous area of memory
consisting of equal-size elements
indexed by contiguous integers.
Constant-time access to any element.
Constant time to add/remove at the
end.
Linear time to add/remove at an
arbitrary location.
Outline
1 Arrays
2 Linked Lists
Singly-Linked List
head
7 10 4 13
7 10 4 13
head
Node contains:
key
next pointer
List API
PushFront(Key) add to front
List API
PushFront(Key) add to front
Key TopFront() return front item
List API
PushFront(Key) add to front
Key TopFront() return front item
PopFront() remove front item
List API
PushFront(Key) add to front
Key TopFront() return front item
PopFront() remove front item
PushBack(Key) add to back
also known as Append
List API
PushFront(Key) add to front
Key TopFront() return front item
PopFront() remove front item
PushBack(Key) add to back
Key TopBack() return back item
List API
PushFront(Key) add to front
Key TopFront() return front item
PopFront() remove front item
PushBack(Key) add to back
Key TopBack() return back item
PopBack() remove back item
List API
PushFront(Key) add to front
Key TopFront() return front item
PopFront() remove front item
PushBack(Key) add to back
Key TopBack() return back item
PopBack() remove back item
Boolean Find(Key) is key in list?
List API
PushFront(Key) add to front
Key TopFront() return front item
PopFront() remove front item
PushBack(Key) add to back
Key TopBack() return back item
PopBack() remove back item
Boolean Find(Key) is key in list?
Erase(Key) remove key from list
List API
PushFront(Key) add to front
Key TopFront() return front item
PopFront() remove front item
PushBack(Key) add to back
Key TopBack() return back item
PopBack() remove back item
Boolean Find(Key) is key in list?
Erase(Key) remove key from list
Boolean Empty() empty list?
List API
PushFront(Key) add to front
Key TopFront() return front item
PopFront() remove front item
PushBack(Key) add to back
Key TopBack() return back item
PopBack() remove back item
Boolean Find(Key) is key in list?
Erase(Key) remove key from list
Boolean Empty() empty list?
AddBefore(Node, Key) adds key before node
List API
PushFront(Key) add to front
Key TopFront() return front item
PopFront() remove front item
PushBack(Key) add to back
Key TopBack() return back item
PopBack() remove back item
Boolean Find(Key) is key in list?
Erase(Key) remove key from list
Boolean Empty() empty list?
AddBefore(Node, Key) adds key before node
AddAfter(Node, Key) adds key after node
Times for Some Operations
7 10 4 13
head
Times for Some Operations
PushFront
7 10 4 13
head
Times for Some Operations
PushFront
7 10 4 13
head
26
Times for Some Operations
PushFront
7 10 4 13
head
26
Times for Some Operations
PushFront O(1)
7 10 4 13
head
26
Times for Some Operations
PopFront
7 10 4 13
head
26
Times for Some Operations
PopFront
7 10 4 13
head
26
Times for Some Operations
PopFront O(1)
7 10 4 13
head
Times for Some Operations
PushBack
(no tail)
7 10 4 13
head
Times for Some Operations
PushBack O(n)
(no tail)
7 10 4 13
head
Times for Some Operations
PopBack
(no tail)
7 10 4 13
head
Times for Some Operations
PopBack O(n)
(no tail)
7 10 4 13
head
Times for Some Operations
7 10 4 13
head
tail
Times for Some Operations
PushBack
(with tail)
7 10 4 13
head
tail
Times for Some Operations
PushBack
(with tail)
7 10 4 13
head
8
tail
Times for Some Operations
PushBack
(with tail)
7 10 4 13
head
8
tail
Times for Some Operations
PushBack O(1)
(with tail)
7 10 4 13
head
8
tail
Times for Some Operations
PopBack
(with tail)
7 10 4 13
head
8
tail
Times for Some Operations
PopBack
(with tail)
7 10 4 13
head
8
tail
Times for Some Operations
PopBack
(with tail)
7 10 4 13
head
8
tail
Times for Some Operations
PopBack O(n)
(with tail)
7 10 4 13
head
tail
Singly-linked List
PushFront(key)
node ←new node
node.key ← key
node.next ← head
head ← node
if tail = nil:
tail ← head
Singly-linked List
PopFront()
if head = nil:
ERROR: empty list
head ← head.next
if head = nil:
tail ← nil
Singly-linked List
PushBack(key)
node ←new node
node.key ← key
node.next =nil
Singly-linked List
PushBack(key)
node ←new node
node.key ← key
node.next =nil
if tail = nil:
head ← tail ← node
Singly-linked List
PushBack(key)
node ←new node
node.key ← key
node.next =nil
if tail = nil:
head ← tail ← node
else:
tail.next ← node
tail ← node
Singly-linked List
PopBack()
Singly-linked List
PopBack()
AddAfter(node, key)
node2 ←new node
node2.key ← key
node2.next = node.next
node.next = node2
if tail = node:
tail ← node2
Singly-Linked List no tail with tail
PushFront(Key) O(1)
Singly-Linked List no tail with tail
PushFront(Key) O(1)
TopFront() O(1)
Singly-Linked List no tail with tail
PushFront(Key) O(1)
TopFront() O(1)
PopFront() O(1)
Singly-Linked List no tail with tail
PushFront(Key) O(1)
TopFront() O(1)
PopFront() O(1)
PushBack(Key) O(n) O(1)
Singly-Linked List no tail with tail
PushFront(Key) O(1)
TopFront() O(1)
PopFront() O(1)
PushBack(Key) O(n) O(1)
TopBack() O(n) O(1)
Singly-Linked List no tail with tail
PushFront(Key) O(1)
TopFront() O(1)
PopFront() O(1)
PushBack(Key) O(n) O(1)
TopBack() O(n) O(1)
PopBack() O(n)
Singly-Linked List no tail with tail
PushFront(Key) O(1)
TopFront() O(1)
PopFront() O(1)
PushBack(Key) O(n) O(1)
TopBack() O(n) O(1)
PopBack() O(n)
Find(Key) O(n)
Singly-Linked List no tail with tail
PushFront(Key) O(1)
TopFront() O(1)
PopFront() O(1)
PushBack(Key) O(n) O(1)
TopBack() O(n) O(1)
PopBack() O(n)
Find(Key) O(n)
Erase(Key) O(n)
Singly-Linked List no tail with tail
PushFront(Key) O(1)
TopFront() O(1)
PopFront() O(1)
PushBack(Key) O(n) O(1)
TopBack() O(n) O(1)
PopBack() O(n)
Find(Key) O(n)
Erase(Key) O(n)
Empty() O(1)
Singly-Linked List no tail with tail
PushFront(Key) O(1)
TopFront() O(1)
PopFront() O(1)
PushBack(Key) O(n) O(1)
TopBack() O(n) O(1)
PopBack() O(n)
Find(Key) O(n)
Erase(Key) O(n)
Empty() O(1)
AddBefore(Node, Key) O(n)
Singly-Linked List no tail with tail
PushFront(Key) O(1)
TopFront() O(1)
PopFront() O(1)
PushBack(Key) O(n) O(1)
TopBack() O(n) O(1)
PopBack() O(n)
Find(Key) O(n)
Erase(Key) O(n)
Empty() O(1)
AddBefore(Node, Key) O(n)
AddAfter(Node, Key) O(1)
Doubly-Linked List
head 7 10 4 13 tail
Doubly-Linked List
head 7 10 4 13 tail
7 10 4 13
head tail
Doubly-Linked List
7 10 4 13
head tail
Node contains:
key
next pointer
prev pointer
Doubly-Linked List
7 10 4 13
head tail
PopBack
Doubly-Linked List
7 10 4 13
head tail
PopBack
Doubly-Linked List
7 10 4 13
head tail
PopBack
Doubly-Linked List
7 10 4
head tail
PopBack
Doubly-Linked List
7 10 4
head tail
PopBack O(1)
Doubly-linked List
PushBack(key)
node ←new node
node.key ← key; node.next =nil
Doubly-linked List
PushBack(key)
node ←new node
node.key ← key; node.next =nil
if tail = nil:
head ← tail ← node
node.prev ←nil
Doubly-linked List
PushBack(key)
node ←new node
node.key ← key; node.next =nil
if tail = nil:
head ← tail ← node
node.prev ←nil
else:
tail.next ← node
node.prev ← tail
tail ← node
Doubly-linked List
PopBack()
Doubly-linked List
PopBack()