Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked Lists.

Similar presentations


Presentation on theme: "Linked Lists."— Presentation transcript:

1 Linked Lists

2 Array List Issues Painful insert/remove at start/middle

3 Array List Issues Painful insert/remove at start/middle
Average ~n/2 wasted space Worse if shrinks

4 Array List Issues Painful insert/remove at start/middle
Up to n wasted space (if just grew) Worse if shrinks Data kept in a single block Memory fragmentation may prevent grow(): array other data

5 Linked List Linked List : Implement List ADT with linked series of nodes One value per node Pointer to next node

6 Linked List Implementation will change performance Memory
+ No extra space for future growth - Extra required for pointers : 4/8 bytes per node

7 Linked List Implementation will change performance
Memory allocated in lots of pieces + no worries about allocating big blocks - lots of calls to new/delete - poorer cache use

8 Linked List Implementation will change performance BigO's will change…

9 Linked Nodes List Node: Stores one item of data
Stores a pointer to another node

10 Linked Nodes Linked ListNodes:
Each node starts with nullptr for next value

11 Linked List Nodes Linked ListNodes:
Each node starts with nullptr for next value Set next to address of other node to form a chain End of chain points to null

12 Nodes vs Pointers new Node: Creating a new permanent thing
Need a pointer to keep track of it Need to delete at some point

13 Nodes vs Pointers pointer variable: May point at May need to delete
New memory Existing memory May need to delete Only if “owns” memory

14 Linked List LinkedList : Object that manages linked ListNode chain
Minimal implementation: Where first node is

15 Traversal Traverse Linked Lists with pointer Start at head

16 Traversal Traverse Linked Lists with pointer Start at head
Advance by following next

17 Traversal Traverse Linked Lists with pointer Start at head
Advance by following next

18 Traversal Traverse Linked Lists with pointer Start at head
Advance by following next Stop when you hit null

19 Putting It Together Print each item:

20 listSize int listSize() If size not stored…
Find by traversing and counting: int listSize() count = 0 current = head while current is not null count++ current = current->next return count

21 Partial Traversal Which node is at index 2?
How do we retrieve the value?

22 Partial Traversal Which node is at index 2?
How do we retrieve the value? Start from head (0) Advance to next twice Return data

23 Partial Traversal retrieveAt(num)
Start from head (0) Advance to next num times Print what is there

24 Partial Traversal retrieveAt(num)
Start from head (0) Advance to next num times If hit nullptr, error Print what is there currentnext on nullptr will blow up

25 Remove Start How do we remove the first node?

26 Remove Start Bad attempt 1: head  head->next node1 is now garbage
need to delete but no pointer!

27 Remove Start Bad attempt 2: delete head Lost track of node 2

28 Remove Start Can't delete node1 unless we store link first
RemoveFirst() if head == nullptr error Node* temp = head->next delete head head = temp

29 Remove Start Can't delete node1 unless we store link first
RemoveFirst() if head == nullptr error Node* temp = head->next delete head head = temp

30 Remove Start Can't delete node1 unless we store link first
RemoveFirst() if head == nullptr error Node* temp = head->next delete head head = temp

31 Insert Start Now we want to add 12 to start of list…

32 Insert Start Now we want to add 12 to start of list
Need a new Node InsertFirst(12) Node* temp = new Node(12)

33 Insert Start Now we want to add 12 to start of list
new node will point to old head InsertFirst(12) Node* temp = new Node(12) temp->next = head

34 Insert Start Now we want to add 12 to start of list
head can then point to temp InsertFirst(12) Node* temp = new Node(12) temp->next = head head = temp

35 Insert At Want to insert a new value at index 2

36 Insert At Want to insert a new value at index 2 InsertAt(index, value)
Node* newNode = new Node(value)

37 Insert At Want to insert a new value at index 2 InsertAt(index, value)
Node* newNode = new Node(value) Node* temp = head

38 Insert At Want to insert a new value at index 2 InsertAt(index, value)
Node* newNode = new Node(value) Node* temp = head Advance to next 2 times

39 Insert At Want to insert a new value at index 2 InsertAt(index, value)
Node* newNode = new Node(value) Node* temp = head Advance to next 2 times TOO FAR No way to adjust node2's pointer!

40 Insert At Want to insert a new value at index 2 InsertAt(index, value)
Node* newNode = new Node(value) Node* temp = head Advance to next 1 times

41 Insert At Want to insert a new value at index 2 InsertAt(index, value)
Node* newNode = new Node(value) Node* temp = head Advance to next 1 times newNode->next = temp->next

42 Insert At Want to insert a new value at index 2 InsertAt(index, value)
Node* newNode = new Node(value) Node* temp = head Advance to next 1 times newNode->next = temp->next temp->next = newNode

43 Insert At Want to insert a new value at index 2 InsertAt(index, value)
Node* newNode = new Node(value) Node* temp = head Advance to next (index – 1) times newNode->next = temp->next temp->next = newNode What special case still won't work?

44 Insert At Want to insert a new value at index InsertAt(index, value)
If index = InsertFirst(value) Else Node* newNode = new Node(value) Node* temp = head Advance to next (index – 1) times If temp == nullptr error newNode->next = temp->next temp->next = newNode

45 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

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


Download ppt "Linked Lists."

Similar presentations


Ads by Google