Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations?...

31
Linked List Improvements & Memory

Transcript of Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations?...

Page 1: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

Linked List Improvements& Memory

Page 2: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

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

Page 3: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

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)

Page 4: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

Linked List Gripes

• Issues with basic linked list:– No easy way to get length– No easy way to put something at the end

Page 5: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

Length

• Keep track of length of list– Increment on any insert– Decrement on removal

• listSize() now O(1)

Page 6: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

Length Side Benefit

• Cheap list size can simplify other algorithms– Error check early

Page 7: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

InsertEnd

• Want to efficiently implement:InsertEnd(int value)

• What do we need?

Page 8: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

Tail Pointer

• Tail pointer– Always points to last element

Page 9: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

InsertEndInsertEnd(value)Node* newNode = new Node(value)tail->next = newNodetail = newNodelength++

Page 10: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

InsertEndInsertEnd(value)Node* newNode = new Node(value)tail->next = newNodetail = newNodelength++

Page 11: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

InsertEndInsertEnd(value)Node* newNode = new Node(value)tail->next = newNodetail = newNodelength++

Page 12: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

InsertEndInsertEnd(value)Node* newNode = new Node(value)tail->next = newNodetail = newNodelength++

Page 13: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

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++

Page 14: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

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

Page 15: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

Tail Issues

• Example:insertStart on empty list

Page 16: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

Tail Issues

• ExampleRemoving at current tail

removeAt(2) calledFound current and nodeToRemove

Page 17: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

Tail Issues

• ExampleRemoving at current tail

removeAt(2) calledRemoved node from list

Page 18: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

Tail Issues

• ExampleRemoving at current tail

removeAt(2) calledDeleted from memory

Page 19: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

Tail Issues

• ExampleRemoving at current tail

removeAt(2) calledWe have a new tail!

Page 20: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

Memory Management

• Destructor needs to free allocated nodes– Must delete each individually

Page 21: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

Destruct~LinkedList()Node* temp = headwhile head is not null

head = head->nextdelete temptemp = head

Page 22: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

Destruct~LinkedList()Node* temp = headwhile head is not null

head = head->nextdelete temptemp = head

Page 23: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

Destruct~LinkedList()Node* temp = headwhile head is not null

head = head->nextdelete temptemp = head

Page 24: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

Destruct~LinkedList()Node* temp = headwhile head is not null

head = head->nextdelete temptemp = head

Page 25: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

Destruct Alternative

• Destructor made simple:

~LinkedList()while head is not null

removeFirst

Page 26: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

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

Page 27: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

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

Page 28: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

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

Page 29: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

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

Page 30: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

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

Page 31: Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

Assignment Operator• myList = other

– Delete nodes 4 and 5– Copy nodes from other