Let's take the above example of the following list:
A->B->C->E->(rest of the list)
You want to insert D.
First you have to get a node* to point to C by going first to A, then following the node* next pointers until you get to C. So let's look at the situation:
Before the insert:
- C:
-
Has some data (C)
-
next points to E
-
-
E:
-
Has some data (E)
-
next points somewhere (it doesn't matter where)
-
-
D (node to be inserted):
-
Has some data (D)
-
next points nowhere (null)
And after the insert:
- C:
-
Has some data (C)
-
next points to D
-
-
E:
-
Has some data (E)
-
next points somewhere (it doesn't matter where)
-
-
D (node to be inserted):
-
Has some data (D)
-
next points to E
Hmm...only two things changed: C's next pointer, and D's next pointer. The data didn't change anywhere, and E wasn't changed.
Can you figure it out from here? It's just a few simple assignment statements.