Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked List Improvements

Similar presentations


Presentation on theme: "Linked List Improvements"— Presentation transcript:

1 Linked List Improvements

2 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) 

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

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

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

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

7 Tail Pointer Tail pointer Always points to last element

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

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

13 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

14 Tail Issues Example: insertStart on empty list

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

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

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

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

19 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

20 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)

21 Linked List & Memory

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

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 ~LinkedList() Node* temp = head while head is not null head = head->next delete temp temp = head

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

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

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

32 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

33 Assignment Operator myList = other Delete all nodes
Copy nodes from other


Download ppt "Linked List Improvements"

Similar presentations


Ads by Google