Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value."— Presentation transcript:

1 Linked List Improvements & Memory

2 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

3 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(1)O(n) O(1) O(n)

4 Linked List Gripes Issues with basic linked list: – No easy way to get length – No easy way to put something at the end

5 Length Keep track of length of list – Increment on any insert – Decrement on removal listSize() now O(1)

6 Length Side Benefit Cheap list size can simplify other algorithms – Error check early

7 InsertEnd Want to efficiently implement: InsertEnd(int value) What do we need?

8 Tail Pointer Tail pointer – Always points to last element

9 InsertEnd InsertEnd(value) Node* newNode = new Node(value) tail->next = newNode tail = newNode length++

10 InsertEnd InsertEnd(value) Node* newNode = new Node(value) tail->next = newNode tail = newNode length++

11 InsertEnd InsertEnd(value) Node* newNode = new Node(value) tail->next = newNode tail = newNode length++

12 InsertEnd InsertEnd(value) Node* newNode = new Node(value) tail->next = newNode tail = newNode length++

13 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 = newNode tail = newNode length++

14 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

15 Tail Issues Example: insertStart on empty list

16 Tail Issues Example Removing at current tail removeAt(2) called Found current and nodeToRemove

17 Tail Issues Example Removing at current tail removeAt(2) called Removed node from list

18 Tail Issues Example Removing at current tail removeAt(2) called Deleted from memory

19 Tail Issues Example Removing at current tail removeAt(2) called We have a new tail!

20 Memory Management Destructor needs to free allocated nodes – Must delete each individually

21 Destruct ~LinkedList() Node* temp = head while head is not null head = head->next delete temp temp = head

22 Destruct ~LinkedList() Node* temp = head while head is not null head = head->next delete temp temp = head

23 Destruct ~LinkedList() Node* temp = head while head is not null head = head->next delete temp temp = head

24 Destruct ~LinkedList() Node* temp = head while head is not null head = head->next delete temp temp = head

25 Destruct Alternative Destructor made simple: ~LinkedList() while head is not null removeFirst

26 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

27 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

28 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

29 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

30 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 …

31 Assignment Operator myList = other – Delete nodes 4 and 5 – Copy nodes from other


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

Similar presentations


Ads by Google