basic data structures: arrays and linked lists · 2016-07-05 · what’s special about arrays?...
TRANSCRIPT
![Page 1: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/1.jpg)
Basic Data Structures:Arrays and Linked Lists
Neil RhodesDepartment of Computer Science and Engineering
University of California, San Diego
Data Structureshttp://bit.ly/algospecialization
![Page 2: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/2.jpg)
Outline
![Page 3: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/3.jpg)
long arr[5];
long arr[] = new long[5];
arr = [None] * 5
1 5 17 3 25 1 5 17 3 258 2 36 5 3
![Page 4: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/4.jpg)
DefinitionArray:Contiguous area of memory
consisting ofequal-size elements indexed by contiguousintegers.
![Page 5: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/5.jpg)
DefinitionArray:Contiguous area of memory consisting ofequal-size elements
indexed by contiguousintegers.
![Page 6: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/6.jpg)
DefinitionArray:Contiguous area of memory consisting ofequal-size elements indexed by contiguousintegers.
1 2 3 4 5 6 7
![Page 7: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/7.jpg)
What’s Special About Arrays?
Constant-time accessarray_addr + elem_size× (i − first_index)
1 2 3 4 5 6 7
![Page 8: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/8.jpg)
What’s Special About Arrays?
Constant-time access
array_addr + elem_size× (i − first_index)
1 2 3 4 5 6 7
![Page 9: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/9.jpg)
What’s Special About Arrays?
Constant-time accessarray_addr
+ elem_size× (i − first_index)
1 2 3 4 5 6 7
![Page 10: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/10.jpg)
What’s Special About Arrays?
Constant-time accessarray_addr + elem_size× (
i − first_index
)
1 2 3 4 5 6 7
![Page 11: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/11.jpg)
What’s Special About Arrays?
Constant-time accessarray_addr + elem_size× (i − first_index)
1 2 3 4 5 6 7
![Page 12: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/12.jpg)
Multi-Dimensional Arrays
(1, 1)
(3,4)
array_addr +elem_size× ((3− 1)× 6 + (4− 1))
![Page 13: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/13.jpg)
Multi-Dimensional Arrays
(1, 1)
(3,4)
array_addr +elem_size× ((3− 1)× 6 + (4− 1))
![Page 14: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/14.jpg)
Multi-Dimensional Arrays
(1, 1)
(3,4)
array_addr +elem_size× ((3− 1)× 6 + (4− 1))
![Page 15: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/15.jpg)
Multi-Dimensional Arrays
(1, 1)
(3,4)
array_addr +elem_size× (
(3− 1)× 6
+ (4− 1))
![Page 16: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/16.jpg)
Multi-Dimensional Arrays
(1, 1)
(3,4)
array_addr +elem_size× (
(3− 1)× 6 + (4− 1)
)
![Page 17: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/17.jpg)
Multi-Dimensional Arrays
(1, 1)
(3,4)
array_addr +
elem_size× ((3− 1)× 6 + (4− 1))
![Page 18: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/18.jpg)
Multi-Dimensional Arrays
(1, 1)
(3,4)
array_addr +elem_size× ((3− 1)× 6 + (4− 1))
![Page 19: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/19.jpg)
(1, 1)
(1, 2)
(1, 3)
(1, 4)
(1, 5)
(1, 6)
(2, 1)...
Row-major
(1, 1)
(2, 1)
(3, 1)
(1, 2)
(2, 2)
(3, 2)
(1, 3)...
Column-major
![Page 20: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/20.jpg)
(1, 1)
(1, 2)
(1, 3)
(1, 4)
(1, 5)
(1, 6)
(2, 1)...
Row-major
(1, 1)
(2, 1)
(3, 1)
(1, 2)
(2, 2)
(3, 2)
(1, 3)...
Column-major
![Page 21: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/21.jpg)
(1, 1)
(1, 2)
(1, 3)
(1, 4)
(1, 5)
(1, 6)
(2, 1)...
Row-major
(1, 1)
(2, 1)
(3, 1)
(1, 2)
(2, 2)
(3, 2)
(1, 3)...
Column-major
![Page 22: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/22.jpg)
(1, 1)
(1, 2)
(1, 3)
(1, 4)
(1, 5)
(1, 6)
(2, 1)...
Row-major
(1, 1)
(2, 1)
(3, 1)
(1, 2)
(2, 2)
(3, 2)
(1, 3)...
Column-major
![Page 23: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/23.jpg)
Times for Common Operations
Add RemoveBeginning
O(n) O(n)
End
O(1) O(1)
Middle
O(n) O(n)
5 8 3 125 8 3 12 45 8 3 12 45 8 3 128 3 128 3 128 3 128 3 12
![Page 24: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/24.jpg)
Times for Common Operations
Add RemoveBeginning
O(n) O(n)
End
O(1) O(1)
Middle
O(n) O(n)
5 8 3 12
5 8 3 12 45 8 3 12 45 8 3 128 3 128 3 128 3 128 3 12
![Page 25: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/25.jpg)
Times for Common Operations
Add RemoveBeginning
O(n) O(n)
End O(1)
O(1)
Middle
O(n) O(n)
5 8 3 12
5 8 3 12 4
5 8 3 12 45 8 3 128 3 128 3 128 3 128 3 12
![Page 26: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/26.jpg)
Times for Common Operations
Add RemoveBeginning
O(n) O(n)
End O(1)
O(1)
Middle
O(n) O(n)
5 8 3 125 8 3 12 4
5 8 3 12 4
5 8 3 128 3 128 3 128 3 128 3 12
![Page 27: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/27.jpg)
Times for Common Operations
Add RemoveBeginning
O(n) O(n)
End O(1) O(1)Middle
O(n) O(n)
5 8 3 125 8 3 12 45 8 3 12 4
5 8 3 12
8 3 128 3 128 3 128 3 12
![Page 28: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/28.jpg)
Times for Common Operations
Add RemoveBeginning
O(n)
O(n)
End O(1) O(1)Middle
O(n) O(n)
5 8 3 125 8 3 12 45 8 3 12 45 8 3 12
8 3 12
8 3 128 3 128 3 12
![Page 29: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/29.jpg)
Times for Common Operations
Add RemoveBeginning
O(n)
O(n)
End O(1) O(1)Middle
O(n) O(n)
5 8 3 125 8 3 12 45 8 3 12 45 8 3 128 3 12
8 3 12
8 3 128 3 12
![Page 30: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/30.jpg)
Times for Common Operations
Add RemoveBeginning
O(n)
O(n)
End O(1) O(1)Middle
O(n) O(n)
5 8 3 125 8 3 12 45 8 3 12 45 8 3 128 3 128 3 12
8 3 12
8 3 12
![Page 31: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/31.jpg)
Times for Common Operations
Add RemoveBeginning
O(n)
O(n)
End O(1) O(1)Middle
O(n) O(n)
5 8 3 125 8 3 12 45 8 3 12 45 8 3 128 3 128 3 128 3 12
8 3 12
![Page 32: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/32.jpg)
Times for Common Operations
Add RemoveBeginning O(n) O(n)
End O(1) O(1)Middle
O(n) O(n)
5 8 3 125 8 3 12 45 8 3 12 45 8 3 128 3 128 3 128 3 12
8 3 12
![Page 33: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/33.jpg)
Times for Common Operations
Add RemoveBeginning O(n) O(n)
End O(1) O(1)Middle O(n) O(n)
5 8 3 125 8 3 12 45 8 3 12 45 8 3 128 3 128 3 128 3 12
8 3 12
![Page 34: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/34.jpg)
Summary
Array: contiguous area of memoryconsisting of equal-size elements indexedby contiguous integers.Constant-time access to any element.Constant time to add/remove at theend.Linear time to add/remove at anarbitrary location.
![Page 35: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/35.jpg)
Summary
Array: contiguous area of memoryconsisting of equal-size elements indexedby contiguous integers.
Constant-time access to any element.Constant time to add/remove at theend.Linear time to add/remove at anarbitrary location.
![Page 36: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/36.jpg)
Summary
Array: contiguous area of memoryconsisting of equal-size elements indexedby contiguous integers.Constant-time access to any element.
Constant time to add/remove at theend.Linear time to add/remove at anarbitrary location.
![Page 37: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/37.jpg)
Summary
Array: contiguous area of memoryconsisting of equal-size elements indexedby contiguous integers.Constant-time access to any element.Constant time to add/remove at theend.
Linear time to add/remove at anarbitrary location.
![Page 38: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/38.jpg)
Summary
Array: contiguous area of memoryconsisting of equal-size elements indexedby contiguous integers.Constant-time access to any element.Constant time to add/remove at theend.Linear time to add/remove at anarbitrary location.
![Page 39: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/39.jpg)
Outline
![Page 40: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/40.jpg)
Singly-Linked List
head
7 10 4 13
head7 10 4 13
Node contains:keynext pointer
![Page 41: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/41.jpg)
List APIPushFront(Key) add to front
Key TopFront() return front itemPopFront() remove front itemPushBack(Key) add to backKey TopBack() return back itemPopBack() remove back itemBoolean Find(Key) is key in list?Erase(Key) remove key from listBoolean Empty() empty list?AddBefore(Node, Key) adds key before nodeAddAfter(Node, Key) adds key after node
![Page 42: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/42.jpg)
List APIPushFront(Key) add to frontKey TopFront() return front item
PopFront() remove front itemPushBack(Key) add to backKey TopBack() return back itemPopBack() remove back itemBoolean Find(Key) is key in list?Erase(Key) remove key from listBoolean Empty() empty list?AddBefore(Node, Key) adds key before nodeAddAfter(Node, Key) adds key after node
![Page 43: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/43.jpg)
List APIPushFront(Key) add to frontKey TopFront() return front itemPopFront() remove front item
PushBack(Key) add to backKey TopBack() return back itemPopBack() remove back itemBoolean Find(Key) is key in list?Erase(Key) remove key from listBoolean Empty() empty list?AddBefore(Node, Key) adds key before nodeAddAfter(Node, Key) adds key after node
![Page 44: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/44.jpg)
List APIPushFront(Key) add to frontKey TopFront() return front itemPopFront() remove front itemPushBack(Key) add to back
also known as Append
Key TopBack() return back itemPopBack() remove back itemBoolean Find(Key) is key in list?Erase(Key) remove key from listBoolean Empty() empty list?AddBefore(Node, Key) adds key before nodeAddAfter(Node, Key) adds key after node
![Page 45: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/45.jpg)
List APIPushFront(Key) add to frontKey TopFront() return front itemPopFront() remove front itemPushBack(Key) add to backKey TopBack() return back item
PopBack() remove back itemBoolean Find(Key) is key in list?Erase(Key) remove key from listBoolean Empty() empty list?AddBefore(Node, Key) adds key before nodeAddAfter(Node, Key) adds key after node
![Page 46: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/46.jpg)
List APIPushFront(Key) add to frontKey TopFront() return front itemPopFront() remove front itemPushBack(Key) add to backKey TopBack() return back itemPopBack() remove back item
Boolean Find(Key) is key in list?Erase(Key) remove key from listBoolean Empty() empty list?AddBefore(Node, Key) adds key before nodeAddAfter(Node, Key) adds key after node
![Page 47: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/47.jpg)
List APIPushFront(Key) add to frontKey TopFront() return front itemPopFront() remove front itemPushBack(Key) add to backKey TopBack() return back itemPopBack() remove back itemBoolean Find(Key) is key in list?
Erase(Key) remove key from listBoolean Empty() empty list?AddBefore(Node, Key) adds key before nodeAddAfter(Node, Key) adds key after node
![Page 48: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/48.jpg)
List APIPushFront(Key) add to frontKey TopFront() return front itemPopFront() remove front itemPushBack(Key) add to backKey TopBack() return back itemPopBack() remove back itemBoolean Find(Key) is key in list?Erase(Key) remove key from list
Boolean Empty() empty list?AddBefore(Node, Key) adds key before nodeAddAfter(Node, Key) adds key after node
![Page 49: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/49.jpg)
List APIPushFront(Key) add to frontKey TopFront() return front itemPopFront() remove front itemPushBack(Key) add to backKey TopBack() return back itemPopBack() remove back itemBoolean Find(Key) is key in list?Erase(Key) remove key from listBoolean Empty() empty list?
AddBefore(Node, Key) adds key before nodeAddAfter(Node, Key) adds key after node
![Page 50: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/50.jpg)
List APIPushFront(Key) add to frontKey TopFront() return front itemPopFront() remove front itemPushBack(Key) add to backKey TopBack() return back itemPopBack() remove back itemBoolean Find(Key) is key in list?Erase(Key) remove key from listBoolean Empty() empty list?AddBefore(Node, Key) adds key before node
AddAfter(Node, Key) adds key after node
![Page 51: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/51.jpg)
List APIPushFront(Key) add to frontKey TopFront() return front itemPopFront() remove front itemPushBack(Key) add to backKey TopBack() return back itemPopBack() remove back itemBoolean Find(Key) is key in list?Erase(Key) remove key from listBoolean Empty() empty list?AddBefore(Node, Key) adds key before nodeAddAfter(Node, Key) adds key after node
![Page 52: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/52.jpg)
Times for Some Operations
head7 10 4 13
26
tail
8
![Page 53: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/53.jpg)
Times for Some Operations
PushFront
head7 10 4 13
26
tail
8
![Page 54: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/54.jpg)
Times for Some Operations
PushFront
head7 10 4 13
26
tail
8
![Page 55: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/55.jpg)
Times for Some Operations
PushFront
head7 10 4 13
26
tail
8
![Page 56: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/56.jpg)
Times for Some Operations
PushFront O(1)
head7 10 4 13
26
tail
8
![Page 57: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/57.jpg)
Times for Some Operations
PopFront
head7 10 4 13
26
tail
8
![Page 58: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/58.jpg)
Times for Some Operations
PopFront
head7 10 4 13
26
tail
8
![Page 59: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/59.jpg)
Times for Some Operations
PopFront O(1)
head7 10 4 13
26
tail
8
![Page 60: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/60.jpg)
Times for Some Operations
PushBack(no tail)
head7 10 4 13
26
tail
8
![Page 61: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/61.jpg)
Times for Some Operations
PushBack O(n)
(no tail)
head7 10 4 13
26
tail
8
![Page 62: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/62.jpg)
Times for Some Operations
PopBack(no tail)
head7 10 4 13
26
tail
8
![Page 63: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/63.jpg)
Times for Some Operations
PopBack O(n)
(no tail)
head7 10 4 13
26
tail
8
![Page 64: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/64.jpg)
Times for Some Operations
head7 10 4 13
26
tail
8
![Page 65: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/65.jpg)
Times for Some Operations
PushBack(with tail)
head7 10 4 13
26
tail
8
![Page 66: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/66.jpg)
Times for Some Operations
PushBack(with tail)
head7 10 4 13
26
tail
8
![Page 67: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/67.jpg)
Times for Some Operations
PushBack(with tail)
head7 10 4 13
26
tail
8
![Page 68: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/68.jpg)
Times for Some Operations
PushBack O(1)(with tail)
head7 10 4 13
26
tail
8
![Page 69: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/69.jpg)
Times for Some Operations
PopBack(with tail)
head7 10 4 13
26
tail
8
![Page 70: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/70.jpg)
Times for Some Operations
PopBack(with tail)
head7 10 4 13
26
tail
8
![Page 71: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/71.jpg)
Times for Some Operations
PopBack(with tail)
head7 10 4 13
26
tail
8
![Page 72: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/72.jpg)
Times for Some Operations
PopBack O(n)
(with tail)
head7 10 4 13
26
tail
8
![Page 73: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/73.jpg)
Singly-linked List
PushFront(key)
node ←new nodenode.key ← key
node.next ← head
head ← node
if tail = nil:tail ← head
![Page 74: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/74.jpg)
Singly-linked List
PopFront()
if head = nil:ERROR: empty list
head ← head .next
if head = nil:tail ← nil
![Page 75: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/75.jpg)
Singly-linked ListPushBack(key)
node ←new nodenode.key ← key
node.next =nil
if tail = nil:head ← tail ← node
else:tail .next ← node
tail ← node
![Page 76: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/76.jpg)
Singly-linked ListPushBack(key)
node ←new nodenode.key ← key
node.next =nilif tail = nil:
head ← tail ← node
else:tail .next ← node
tail ← node
![Page 77: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/77.jpg)
Singly-linked ListPushBack(key)
node ←new nodenode.key ← key
node.next =nilif tail = nil:
head ← tail ← node
else:tail .next ← node
tail ← node
![Page 78: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/78.jpg)
Singly-linked ListPopBack()
if head = nil: ERROR: empty listif head = tail:
head ← tail ←nilelse:
p ← head
while p.next.next ̸= nil:p ← p.next
p.next ← nil; tail ← p
![Page 79: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/79.jpg)
Singly-linked ListPopBack()
if head = nil: ERROR: empty list
if head = tail:head ← tail ←nil
else:p ← head
while p.next.next ̸= nil:p ← p.next
p.next ← nil; tail ← p
![Page 80: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/80.jpg)
Singly-linked ListPopBack()
if head = nil: ERROR: empty listif head = tail:
head ← tail ←nil
else:p ← head
while p.next.next ̸= nil:p ← p.next
p.next ← nil; tail ← p
![Page 81: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/81.jpg)
Singly-linked ListPopBack()
if head = nil: ERROR: empty listif head = tail:
head ← tail ←nilelse:
p ← head
while p.next.next ̸= nil:p ← p.next
p.next ← nil; tail ← p
![Page 82: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/82.jpg)
Singly-linked ListPopBack()
if head = nil: ERROR: empty listif head = tail:
head ← tail ←nilelse:
p ← head
while p.next.next ̸= nil:p ← p.next
p.next ← nil; tail ← p
![Page 83: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/83.jpg)
Singly-linked List
AddAfter(node, key)
node2←new nodenode2.key ← key
node2.next = node.next
node.next = node2if tail = node:
tail ← node2
![Page 84: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/84.jpg)
Singly-Linked List no tail with tailPushFront(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)
![Page 85: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/85.jpg)
Singly-Linked List no tail with tailPushFront(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)
![Page 86: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/86.jpg)
Singly-Linked List no tail with tailPushFront(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)
![Page 87: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/87.jpg)
Singly-Linked List no tail with tailPushFront(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)
![Page 88: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/88.jpg)
Singly-Linked List no tail with tailPushFront(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)
![Page 89: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/89.jpg)
Singly-Linked List no tail with tailPushFront(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)
![Page 90: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/90.jpg)
Singly-Linked List no tail with tailPushFront(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)
![Page 91: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/91.jpg)
Singly-Linked List no tail with tailPushFront(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)
![Page 92: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/92.jpg)
Singly-Linked List no tail with tailPushFront(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)
![Page 93: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/93.jpg)
Singly-Linked List no tail with tailPushFront(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)
![Page 94: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/94.jpg)
Singly-Linked List no tail with tailPushFront(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)
![Page 95: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/95.jpg)
Doubly-Linked List
head tail7 10 4 13
![Page 96: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/96.jpg)
Doubly-Linked List
head tail7 10 4 13
head tail
7 10 4 13
![Page 97: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/97.jpg)
Doubly-Linked List
head tail
7 10 4 13
Node contains:keynext pointerprev pointer
![Page 98: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/98.jpg)
Doubly-Linked List
head tail
7 10 4 13
PopBack
![Page 99: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/99.jpg)
Doubly-Linked List
head tail
7 10 4 13
PopBack
![Page 100: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/100.jpg)
Doubly-Linked List
head tail
7 10 4 13
PopBack
![Page 101: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/101.jpg)
Doubly-Linked List
head tail
7 10 4
13
PopBack
![Page 102: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/102.jpg)
Doubly-Linked List
head tail
7 10 4
13
PopBack O(1)
![Page 103: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/103.jpg)
Doubly-linked ListPushBack(key)
node ←new nodenode.key ← key; node.next =nil
if tail = nil:head ← tail ← node
node.prev ←nilelse:
tail .next ← node
node.prev ← tail
tail ← node
![Page 104: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/104.jpg)
Doubly-linked ListPushBack(key)
node ←new nodenode.key ← key; node.next =nilif tail = nil:
head ← tail ← node
node.prev ←nil
else:tail .next ← node
node.prev ← tail
tail ← node
![Page 105: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/105.jpg)
Doubly-linked ListPushBack(key)
node ←new nodenode.key ← key; node.next =nilif tail = nil:
head ← tail ← node
node.prev ←nilelse:
tail .next ← node
node.prev ← tail
tail ← node
![Page 106: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/106.jpg)
Doubly-linked List
PopBack()
if head = nil: ERROR: empty listif head = tail:
head ← tail ←nilelse:
tail ← tail .prev
tail .next ←nil
![Page 107: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/107.jpg)
Doubly-linked List
PopBack()
if head = nil: ERROR: empty list
if head = tail:head ← tail ←nil
else:tail ← tail .prev
tail .next ←nil
![Page 108: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/108.jpg)
Doubly-linked List
PopBack()
if head = nil: ERROR: empty listif head = tail:
head ← tail ←nil
else:tail ← tail .prev
tail .next ←nil
![Page 109: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/109.jpg)
Doubly-linked List
PopBack()
if head = nil: ERROR: empty listif head = tail:
head ← tail ←nilelse:
tail ← tail .prev
tail .next ←nil
![Page 110: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/110.jpg)
Doubly-linked ListAddAfter(node, key)
node2←new nodenode2.key ← key
node2.next ← node.next
node2.prev ← node
node.next ← node2if node2.next ̸=nil:
node2.next.prev ← node2if tail = node:
tail ← node2
![Page 111: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/111.jpg)
Doubly-linked ListAddBefore(node, key)
node2←new nodenode2.key ← key
node2.next ← node
node2.prev ← node.prev
node.prev ← node2if node2.prev ̸=nil:
node2.prev .next ← node2if head = node:
head ← node2
![Page 112: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/112.jpg)
Singly-Linked List no tail with tailPushFront(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)
![Page 113: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/113.jpg)
Doubly-Linked List no tail with tailPushFront(Key) O(1)
TopFront() O(1)PopFront() O(1)
PushBack(Key) O(n) O(1)TopBack() O(n) O(1)PopBack() O(n) O(1)Find(Key) O(n)
Erase(Key) O(n)
Empty() O(1)AddBefore(Node, Key) O(n) O(1)
AddAfter(Node, Key) O(1)
![Page 114: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/114.jpg)
SummaryConstant time to insert at or removefrom the front.
With tail and doubly-linked, constanttime to insert at or remove from theback.O(n) time to find arbitrary element.List elements need not be contiguous.With doubly-linked list, constant time toinsert between nodes or remove a node.
![Page 115: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/115.jpg)
SummaryConstant time to insert at or removefrom the front.With tail and doubly-linked, constanttime to insert at or remove from theback.
O(n) time to find arbitrary element.List elements need not be contiguous.With doubly-linked list, constant time toinsert between nodes or remove a node.
![Page 116: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/116.jpg)
SummaryConstant time to insert at or removefrom the front.With tail and doubly-linked, constanttime to insert at or remove from theback.O(n) time to find arbitrary element.
List elements need not be contiguous.With doubly-linked list, constant time toinsert between nodes or remove a node.
![Page 117: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/117.jpg)
SummaryConstant time to insert at or removefrom the front.With tail and doubly-linked, constanttime to insert at or remove from theback.O(n) time to find arbitrary element.List elements need not be contiguous.
With doubly-linked list, constant time toinsert between nodes or remove a node.
![Page 118: Basic Data Structures: Arrays and Linked Lists · 2016-07-05 · What’s Special About Arrays? Constant-timeaccess array_addr+elem_size×(i −first_index) 1 2 3 4 5 6 7](https://reader035.vdocuments.mx/reader035/viewer/2022070810/5f091aea7e708231d4254117/html5/thumbnails/118.jpg)
SummaryConstant time to insert at or removefrom the front.With tail and doubly-linked, constanttime to insert at or remove from theback.O(n) time to find arbitrary element.List elements need not be contiguous.With doubly-linked list, constant time toinsert between nodes or remove a node.