Lecture #17 Need to Insert a Gap…

4 downloads 233 Views 65KB Size Report
Person {“Alan”, 26}. Person {“Becky”, 28}. Person {“Frank”, 21}. Person {“Jack”, 26 }. Person {“Joe”, 28}. Person {“Sally”, 21}. ▫ Want to add "Henry" but Preserve ...
CSE1030 – Lecture #17

CSE1030 – Introduction to Computer Science II

ƒ Review ƒ Introduction to Linked Lists ƒ We’re Done!

Lecture #17 Introduction to Linked Lists

CSE1030 2

Remember the Array Insertion Problem? ƒ Want to add "Henry" but Preserve the Order p

Need to Insert a Gap… ƒ (Which is Time Consuming) p

[0]

Person {“Alan”, 26}

[0]

Person {“Alan”, 26}

[1]

Person {“Becky”, 28}

[1]

Person {“Becky”, 28}

[2]

Person {“Frank”, 21}

[2]

Person {“Frank”, 21}

[3]

Person {“Jack”, 26}

[3]

Person {“Jack”, 26}

[4]

Person {“Joe”, 28}

[4]

Person {“Joe”, 28}

[5]

Person {“Sally”, 21}

[5]

Person {“Sally”, 21}

[6]

null

[6]

[7]

null

[7]

null

[8]

null

[8]

null

null

[9]

null

[9]

CSE1030 3

CSE1030 4

Need to Insert a Gap…

Need to Insert a Gap…

ƒ (Which is Time Consuming)

ƒ (Which is Time Consuming)

p

p

[0]

Person {“Alan”, 26}

[0]

[1]

Person {“Alan”, 26}

Person {“Becky”, 28}

[1]

Person {“Becky”, 28}

[2]

Person {“Frank”, 21}

[2]

Person {“Frank”, 21}

[3]

Person {“Henry”, 26} Person {“Jack”, 26}

[3] [4]

Person {“Jack”, 26}

[4]

[5]

Person {“Joe”, 28}

[5]

Person {“Joe”, 28}

[6]

Person {“Sally”, 21}

[6]

Person {“Sally”, 21}

[7]

null

[7]

null

[8]

null

[8]

null

[9]

null

[9]

null

CSE1030 5

CSE1030 6

Is there a way to do this Without Shifting?

CSE1030 – Lecture #17 ƒ Review ƒ Introduction to Linked Lists ƒ We’re Done!

p

CSE1030 7

[0]

Person {“Alan”, 26}

[1]

Person {“Becky”, 28}

[2]

Person {“Frank”, 21}

[3]

Person {“Jack”, 26}

[4]

Person {“Joe”, 28}

[5]

Person {“Sally”, 21}

[6]

null

[7]

null

[8]

null

[9]

null

ƒ The problem is that the array is a single contiguous block of memory ƒ Instead, if it was a series of little blocks, then we wouldn't have to shift anything…

CSE1030 8

How about we use lots of Little Blocks of Memory, instead of 1 Big one? p

Person {“Alan”, 26}

Person {“Becky”, 28}

ƒ Each Little Block holds an arrow (reference, pointer) to the data

Now, Inserting Henry is Easy! p

Person {“Alan”, 26}

Person {“Becky”, 28}

Person {“Frank”, 21}

Person {“Frank”, 21}

Person {“Jack”, 26}

ƒ This only required changing 2 arrows…

Person {“Henry”, 26}

ƒ Each Little Block also has to provide a way to find the next little block

Person {“Jack”, 26}

Person {“Joe”, 28}

Person {“Joe”, 28}

null

null

ƒ … and adding 1 new little block of memory.

CSE1030 9

CSE1030 10

1 – Create a New Larger Array

Implications

Person[] p_new = new Person[p.length + 5];

p_new

[0]

ƒ We are still storing a collection of arrows (or "references", or "pointers") as we did when we used arrays

[1]

[2]

[3] [4] [5]

ƒ There are other benefits too… CSE1030 11

null null null null null

[7]

null

[8]

null

[9]

ƒ But because the arrows are in their own individual little pieces of memory, nothing has to be shifted to insert new ones

null

[6]

More Implications ƒ Remember what a pain Array Resizing was? ƒ And Inefficient too ƒ There is No Resizing with the New Approach!

null

null

2 – Copy the objects over to the new array

3 – Switch over to the new array p = p_new;

for(int i = 0; i < p.length; i++) p_new[i] = p[i];

p_new

p

[0]

[0]

Person {“Sally”, 26}

[1]

[1]

Person {“Frank”, 28}

[2]

[2]

Person {“Joe”, 21}

[3]

[3]

Person {“Becky”, 26}

[4]

[4]

Person {“Alan”, 28}

[5]

null

[6]

null

[7]

null

[8]

null

[9]

null

p

[0]

Person {“Sally”, 26}

[1]

Person {“Frank”, 28}

[2]

Person {“Joe”, 21}

[3]

Person {“Becky”, 26}

[4]

Person {“Alan”, 28}

p_new

[5]

null

[6]

null

[7]

null

[8]

null

[9]

null

CSE1030 12

Deletion is also Easy p

Negative Implications? Accessing Data p

Person {“Alan”, 26}

Person {“Becky”, 28}

Person {“Frank”, 21}

Person {“Alan”, 26}

ƒ Deleting "Frank" only requires us to update 1 pointer – Fast!

Person {“Becky”, 28}

Person {“Frank”, 21}

Person {“Jack”, 26}

Person {“Jack”, 26}

Person {“Joe”, 28}

Person {“Joe”, 28}

null

CSE1030 13

Arrays versus Linked Lists ƒ Good: ƒ Access to any element is very fast: p[i] ƒ Adding / Deleting from the End is Fastest (but can cause Resizing) ƒ Efficient on Memory (only 1 arrow per Data item) ƒ But empty slots waste memory

ƒ Bad: ƒ Insertion / Deletion anywhere but the end of the array ƒ Resizing

[0]

Person {“Alan”, 26}

[1]

Person {“Becky”, 28}

[2]

Person {“Frank”, 21}

[3]

Person {“Jack”, 26}

[4]

Person {“Joe”, 28}

[5]

Person {“Sally”, 21}

[6]

null

[7]

null

[8]

null

[9]

null

null

Head Pointer

CSE1030 14

Linked List Terminology

ƒ Good:

p

ƒ Insertion / Deletion is easy (just update some arrows) ƒ Insertion or Deletion at the Top of the list is Fastest ƒ There is no "Resizing Cost"

p

Person {“Alan”, 26}

Person {“Becky”, 28}

Head of the List Person {“Frank”, 21}

ƒ Bad:

Data

A Node

ƒ Accessing en element requires us to iterate along the List – Slower than array ƒ Wastes more Memory (2 arrows per Data item) ƒ Although it, doesn't have empty slots CSE1030 15

Person {“Jack”, 26}

Tail of the List

Person {“Joe”, 28}

null

'null' marks the end of the Linked List

CSE1030 16

CSE1030 – Lecture #17 ƒ Review ƒ Introduction to Linked Lists ƒ We’re Done!

Next topic… Linked Lists II

CSE1030 17

CSE1030 18