Download presentation
Presentation is loading. Please wait.
1
Linked List Insert After
CSCE 121
2
Insert 14 after 3 Head 11 3 8
3
1) Traverse to 3 Head 11 3 8 Current
4
1) Traverse to 3 Head 11 3 8 Current
5
2) Create new node NewNode 14 Head 11 3 8 Current
6
3) Connect to list NewNode 14 Head 11 3 8 Current
7
3a) Connect ‘14’ to ‘8’ NewNode 14 Head 11 3 8 Current
8
14 11 3 8 3a) Connect ‘14’ to ‘8’ NewNode Head Current
Set NewNode->next to Current->next. Current
9
3b) Connect ‘3’ to ‘14’ NewNode 14 Head 11 3 8 Current
10
14 11 3 8 3b) Connect ‘3’ to ‘14’ NewNode Head Current
Order in step 3 is IMPORTANT! NewNode 14 Head 11 3 8 Set Current->next to NewNode. Current
11
14 11 3 8 3) Connect to list NewNode Head Current Rewinding to try a
different order! NewNode 14 Head 11 3 8 Current
12
3a) Connect ‘3’ to ‘14’ NewNode 14 Head 11 3 8 Current
13
14 11 3 8 3a) Connect ‘3’ to ‘14’ NewNode Head Current
Set Current->next to NewNode. Current
14
Oops! We don’t have anything pointing to ‘8’!
3b) Connect ‘14’ to ‘8’ NewNode 14 Head Memory Leak! 11 3 8 Oops! We don’t have anything pointing to ‘8’! Current
15
Notes This example only shows the general case.
What happens if you add after head? What happens if you add after tail? What happens if you add after in an empty list?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.