Linked Lists CSC220 Winter 2004-5. Array vs Linked List node Array Linked List.
Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations?...
Transcript of Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations?...
Linked List Improvements& Memory
BigO's
• What is BigO for our basic linked list operations?
InsertStart Insert at middle
InsertEnd Retrieve First Value
Retrieve Middle Value
Remove From End
Remove From
Beginning
Get Length
BigO's
• What is BigO for our basic linked list operations?
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(n) O(1) O(n)
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– 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
InsertEndInsertEnd(value)Node* newNode = new Node(value)tail->next = newNodetail = newNodelength++
InsertEndInsertEnd(value)Node* newNode = new Node(value)tail->next = newNodetail = newNodelength++
InsertEndInsertEnd(value)Node* newNode = new Node(value)tail->next = newNodetail = newNodelength++
InsertEndInsertEnd(value)Node* newNode = new Node(value)tail->next = newNodetail = newNodelength++
InsertEnd• Special case : insertEnd on empty list– Pass off to insertStart
InsertEnd(value)If length == 0
insertStart(value)return
Node* newNode = new Node(value)tail->next = newNodetail = newNodelength++
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
• ExampleRemoving at current tail
removeAt(2) calledFound current and nodeToRemove
Tail Issues
• ExampleRemoving at current tail
removeAt(2) calledRemoved node from list
Tail Issues
• ExampleRemoving at current tail
removeAt(2) calledDeleted from memory
Tail Issues
• ExampleRemoving at current tail
removeAt(2) calledWe have a new tail!
Memory Management
• Destructor needs to free allocated nodes– Must delete each individually
Destruct~LinkedList()Node* temp = headwhile head is not null
head = head->nextdelete temptemp = head
Destruct~LinkedList()Node* temp = headwhile head is not null
head = head->nextdelete temptemp = head
Destruct~LinkedList()Node* temp = headwhile head is not null
head = head->nextdelete temptemp = head
Destruct~LinkedList()Node* temp = headwhile head is not null
head = head->nextdelete temptemp = 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 otherget node's valueinsert 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 otherget node's valueinsert 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 otherget node's valueinsert 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 otherget node's valueinsert 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 otherget node's valueinsert value at end
…
Assignment Operator• myList = other
– Delete nodes 4 and 5– Copy nodes from other