Download presentation
Presentation is loading. Please wait.
Published byMelvyn Francis Modified over 9 years ago
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 Average ~n/2 wasted space – 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 – Space always based on current size of list But worst case larger than array – Data distributed in lots of little pieces + no worries about allocating big blocks - cache coherence – BigO's will change
7
Linked List List Node: – Stores one item of data – Stores a pointer to another node
8
Linked List Linked ListNodes: – Each node starts with nullptr for next value
9
Linked List Linked ListNodes: – Each node starts with nullptr for next value – Set next to address of other node to form a chain – End of chain still points to null
10
Linked List Goal : List ADT implemented with LinkNodes What does list need to keep track of? Bare minimum: Where first node is
11
Traversal Traverse Linked Lists with pointer – Start at head
12
Traversal Traverse Linked Lists with pointer – Start at head – Advance by following next
13
Traversal Traverse Linked Lists with pointer – Start at head – Advance by following next
14
Traversal Traverse Linked Lists with pointer – Start at head – Advance by following next – Stop when you hit null
15
Putting It Together Print each item:
16
listSize Size of list not explicitly stored Find by traversing and counting: int listSize() count = 0 current = head while current is not null count++ current = current->next return count
17
Partial Traversal Which node is at index 2? How do we retrieve the value?
18
Partial Traversal Which node is at index 2? How do we retrieve the value? Start from head (0) Advance to next twice Return data
19
Partial Traversal retrieveAt(num) Start from head (0) Advance to next num times Print what is there
20
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
21
Remove Start How do we remove the first node?
22
Remove Start Bad attempt 1: head head->next – node1 is now garbage need to delete but no pointer!
23
Remove Start Bad attempt 2: delete head – Lost track of node 2
24
Remove Start Can't delete node1 unless we store link first RemoveFirst() if head == nullptr error Node* temp = head->next delete head head = temp
25
Remove Start Can't delete node1 unless we store link first RemoveFirst() if head == nullptr error Node* temp = head->next delete head head = temp
26
Remove Start Can't delete node1 unless we store link first RemoveFirst() if head == nullptr error Node* temp = head->next delete head head = temp
27
Insert Start Now we want to add 12 to start of list…
28
Insert Start Now we want to add 12 to start of list – Need a new Node InsertFirst(12) Node* temp = new Node(12)
29
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
30
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
31
Insert At Want to insert a new value at index 2
32
Insert At Want to insert a new value at index 2 InsertAt(index, value) Node* newNode = new Node(value)
33
Insert At Want to insert a new value at index 2 InsertAt(index, value) Node* newNode = new Node(value) Node* temp = head
34
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
35
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!
36
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
37
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
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 1 times newNode->next = temp->next temp->next = newNode
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 (index – 1) times newNode->next = temp->next temp->next = newNode What special case still won't work?
40
Insert At Want to insert a new value at index InsertAt(index, value) If index = 0 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.