Linked List Improvements
BigO's What is BigO for our basic linked list operations? O(1) O(n) InsertStart Insert at middle InsertEnd Retrieve First Value Retrieve Middle Value Remove From End Remove From Beginning Get Length O(1) O(n) O(n) O(1) O(n) O(1)
Linked List Gripes Issues with basic linked list: No easy way to get length No easy way to put something at the end
Length Keep track of length of list listSize() now O(1) Increment on any insert Decrement on removal listSize() now O(1)
Length Side Benefit Cheap list size can simplify other algorithms Error check early
InsertEnd Want to efficiently implement: InsertEnd(int value) What do we need?
Tail Pointer Tail pointer Always points to last element
InsertEnd InsertEnd(value) Node* newNode = new Node(value) tail->next = newNode tail = newNode length++
InsertEnd InsertEnd(value) Node* newNode = new Node(value) tail->next = newNode tail = newNode length++
InsertEnd InsertEnd(value) Node* newNode = new Node(value) tail->next = newNode tail = newNode length++
InsertEnd InsertEnd(value) Node* newNode = new Node(value) tail->next = newNode tail = newNode length++
InsertEnd Special case : insertEnd on empty list InsertEnd(value) Pass off to insertStart InsertEnd(value) If length == 0 insertStart(value) return Node* newNode = new Node(value) tail->next = newNode tail = newNode length++
Tail Issues InsertEnd(value) now O(1) But need to worry about tail in other situations InsertStart on empty list InsertAt on last node RemoveAt on last node
Tail Issues Example: insertStart on empty list
Tail Issues Example Removing at current tail removeAt(2) called Found current and nodeToRemove
Tail Issues Example Removing at current tail removeAt(2) called Removed node from list
Tail Issues Example Removing at current tail removeAt(2) called Deleted from memory
Tail Issues Example Removing at current tail removeAt(2) called We have a new tail!
BigO's What is BigO for our improved linked list operations? With length & tail Get Length Insert Start Insert at InsertEnd Retrieve Retrieve Remove Remove middle First / L ast Middle Beginning End Value Value
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)
Linked List & Memory
Memory Management Destructor needs to free allocated nodes Must delete each individually
Destruct ~LinkedList() Node* temp = head while head is not null head = head->next delete temp temp = head
Destruct ~LinkedList() Node* temp = head while head is not null head = head->next delete temp temp = head
Destruct ~LinkedList() Node* temp = head while head is not null head = head->next delete temp temp = head
Destruct ~LinkedList() Node* temp = head while head is not null head = head->next delete temp temp = head
Destruct Alternative Destructor made simple: ~LinkedList() while head is not null removeFirst
Copy Constructor Want a deep copy Whole new list of nodes with identical data, in same order Length, head and tail set correctly foreach node in other get node's value insert value at end
Copy Constructor Want a deep copy Whole new list of nodes with identical data, in same order Length, head and tail set correctly foreach node in other get node's value insert value at end
Copy Constructor Want a deep copy Whole new list of nodes with identical data, in same order Length, head and tail set correctly foreach node in other get node's value insert value at end
Copy Constructor Want a deep copy Whole new list of nodes with identical data, in same order Length, head and tail set correctly foreach node in other get node's value insert value at end
Copy Constructor Want a deep copy Whole new list of nodes with identical data, in same order Length, head and tail set correctly foreach node in other get node's value insert value at end …
Assignment Operator myList = other Delete all nodes Copy nodes from other