Professional Documents
Culture Documents
14 LinkedLists
14 LinkedLists
• Linked Lists
• Test (Reminder):
• There will be a 90 minute test
• 15th December 15:00-17:00
• Practice: past tests and exams
available.
1
Where have we been?
Implementing Collections:
• Stack: O(1)
• ArrayQueue: O(1)
• ArraySet: O(n) (cost of searching)
H P
A J
C X
B K M
P
B C
X
K
H
M
J P
A C
X
A C J M P X
• Or with ‘Pointers’:
• Each node contains a reference to the next object
A C J M P X
8
Memory allocation
• Pointers/references contain an address in the computer’s
memory, where the data can be stored. How is this memory
allocated?
• You’ve been doing it (at least) since Comp102, using new to
create an object will allocate some of the computer’s memory for
your object. This memory is called heap memory:
Object o = new Object(); o Object
Object p = o; p
10
Adding Nodes
LinkedNode<String> colours = new LinkedNode<String>(“red”,
null);
colours.setNext(new LinkedNode<String>(“blue”, null));
colours = new LinkedNode<String>(“green”, colours);
“green”
colours
“red” “blue”
colours
“blue”
14