basic data structures: arrays and linked lists · 2016-07-05 · what’s special about arrays?...

118
Basic Data Structures: Arrays and Linked Lists Neil Rhodes Department of Computer Science and Engineering University of California, San Diego Data Structures http://bit.ly/algospecialization

Upload: others

Post on 19-Jun-2020

1 views

Category:

Documents


0 download

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

(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

(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

(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

(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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.