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