Linked List Insert After CSCE 121
Insert 14 after 3 Head 11 3 8
1) Traverse to 3 Head 11 3 8 Current
1) Traverse to 3 Head 11 3 8 Current
2) Create new node NewNode 14 Head 11 3 8 Current
3) Connect to list NewNode 14 Head 11 3 8 Current
3a) Connect ‘14’ to ‘8’ NewNode 14 Head 11 3 8 Current
14 11 3 8 3a) Connect ‘14’ to ‘8’ NewNode Head Current Set NewNode->next to Current->next. Current
3b) Connect ‘3’ to ‘14’ NewNode 14 Head 11 3 8 Current
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
14 11 3 8 3) Connect to list NewNode Head Current Rewinding to try a different order! NewNode 14 Head 11 3 8 Current
3a) Connect ‘3’ to ‘14’ NewNode 14 Head 11 3 8 Current
14 11 3 8 3a) Connect ‘3’ to ‘14’ NewNode Head Current Set Current->next to NewNode. Current
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
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?