Download presentation
Presentation is loading. Please wait.
Published byLiana Indradjaja Modified over 6 years ago
1
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
2
Dummy Nodes
3
Plain Linked List Linked List has annoying special cases:
InsertAt(index, value) If index = InsertFirst(value) Else Node* newNode = new Node(value) Node* temp = head Repeat (index – 1) times temp = temp->next If temp == nullptr error newNode->next = temp->next temp->next = newNode
4
Plain Linked List Empty List: List with 1 value: Solution : Dummy Node
ListNode that head always points to Created at construction, does not count in length Empty List: List with 1 value:
5
Plain Linked List Eliminates special cases
InsertAt(index, value) Node* newNode = new Node(value) Node* temp = head Repeat index times temp = temp->next If temp == nullptr error newNode->next = temp->next temp->next = newNode
6
Linked List Directional Challenges
7
BigO's What is BigO for our improved linked list operations? O(1) O(1)
With length & tail Get Length Insert Start Insert at InsertEnd Retrieve Retrieve Remove Remove middle First / L ast Middle Beginning End Value Value O(1) O(1) O(n) O(1) O(1) O(n) O(1) O(n)
8
Retrieve First / Last Value
BigO's What is BigO for our improved linked list operations? With length & tail Get Length Insert Start Insert at middle InsertEnd Retrieve First / Last Value Retrieve Middle Value Remove Beginning Remove End O(1) O(1) O(n)
9
Reverse Traverse What if we want to reverse through the list?
E.g. Print in reverse order
10
Reverse Traverse What if we want to print the list in reverse order? Iterative: for(i = length-1 … 0) cout << retrieveAt(i) BigO?
11
Reverse Traverse What if we want to print the list in reverse order? Iterative: for(i = length-1 … 0) //O(n) cout << retrieveAt(i) //O(n) BigO? O(n2)
12
Lesson Access middle: Avoid retrieveAt(i) in linked list!
ArrayList : O(1) LinkedList : O(n) Avoid retrieveAt(i) in linked list!
13
Doubly Linked Lists
14
Doubly Linked Node Doubly linked node Pointers to previous and next
15
Doubly Linked List Doubly Linked List
16
With Dummies Dummy/Sentinel nodes to avoid special cases
17
Construct Valid starting state Head/Tail dummy nodes Head Tail
prev : null next : tail Tail prev : head next : null Length : 0
18
Inserting InsertAt(index, value) : Insert(0, 8)
previous = head repeat index times previous = previous->next make newNode
19
Inserting InsertAt(index, value) : Insert(0, 8)
previous = head repeat index times previous = previous->next make newNode attach newNode to prev and prev->next
20
Inserting InsertAt(index, value) : Insert(0, 8)
previous = head repeat index times previous = previous->next make newNode attach newNode to prev and prev->next attach newNode neighbors to newNode
21
Inserting InsertAt(index, value) : Insert(0, 8)
previous = head repeat index times previous = previous->next make newNode attach newNode to prev and prev->next attach newNode neighbors to newNode update length
22
Inserting InsertAt(2, 15) previous = head repeat index times previous = previous->next make newNode attach newNode to prev and prev->next attach newNode neighbors to newNode update length
23
Inserting InsertAt(2, 15) previous = head repeat index times previous = previous->next make newNode attach newNode to prev and prev->next attach newNode neighbors to newNode update length
24
Inserting InsertAt(2, 15) previous = head repeat index times previous = previous->next make newNode attach newNode to prev and prev->next attach newNode neighbors to newNode update length
25
Inserting InsertAt(2, 15) previous = head repeat index times previous = previous->next make newNode attach newNode to prev and prev->next attach newNode neighbors to newNode update length
26
Removal RemoveAt(index) : RemoveAt(1)
if index < 0 or index >= length, error current = head->next repeat index times current = current >next attach current's neighbors to each other delete current update length
27
Removal RemoveAt(index) : RemoveAt(1)
if index < 0 or index >= length, error current = head->next repeat index times current = current >next attach current's neighbors to each other delete current update length
28
Removal RemoveAt(index) : RemoveAt(1)
if index < 0 or index >= length, error current = head->next repeat index times current = current >next attach current's neighbors to each other delete current update length
29
Removal RemoveAt(index) : RemoveAt(1)
if index < 0 or index >= length, error current = head->next repeat index times current = current >next attach current's neighbors to each other delete current update length
30
Final BigO Big O's for Doubly Linked O(1) O(1) O(n) Get Length
Anything at start or end Anything in middle Forward Traversal of list Reverse Traversal of list O(1) O(1) O(n)
31
Circular List Circular linked list Last node links back to first
Used to model domains, not for efficiency
32
Doubly Linked Tasks Implement: Constructor InsertAt RemoveAt
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.