Professional Documents
Culture Documents
16 DoublyLinkedLists
16 DoublyLinkedLists
16 DoublyLinkedLists
• Test (Reminder):
• There will be a 90 minute test
• Friday 15th at 3-5pm
• EA006
1
Fi ndi ng Nodes in a
SLL
If we use a list for ‘general’ data, we need operations to
find, add and remove.
Find should traverse the list until it finds the target item:
Unordered List Ordered List
Ordered List
5
Modi fie d Fi nd
G
E I
?
A
K
7
Doubly Linked Lists
• To avoid having to traverse the list, we can
simply store a previous pointer in the node.
Doubly
Linked List
3 1 5 4 5
• Advantages:
• We can now implement the find, add and remove
operations much more elegantly (reuse of find).
• We can now manipulate the list using references (say
from an index).
• List can be traversed in either direction (i.e. browser
pages)
• Disadvantages:
• more effort in updating an extra set of pointers.
• More space required to store those pointers. 8
Remove Example DLL
ref.remove()
ref.prev.next = ref.next
ref.next.prev = ref.prev
ref
list
G
E
A I
9
Add Exercise DLL
• Write the code to insert E back into the list before ref.
• Note: The first and last node in a list is always a special
case (in both SLL and DLL), don’t worry about this.
ref
E E
list
A I
10
Solution
11