Get five coat hangers. Consider each of them one variable of type Node
(struct Node
is the same, just in a C-style of syntax, which a C++ compiler also accepts).
Your hand is the pointer first
(of type Node *
in C++ syntax and struct Node *
in the C-style).
Your hand being still empty is like first
being NULL.
Take one of the coat hangers in your hand. (Hold it on one end, not on the hook.)
Now first
points to the first hanger. (Hold the first hanger with the hook pointing down.)
Hang the second hanger into the hook of the first, so that the second hook is empty and pointing down. You have added the second element to your list, by making the hook/pointer of the first hold the second. The hook of the second is empty, NULL.
Hang the third hanger into the second, third hook pointing down and empty.
Add the fourth likewise.
Your second hand, if you ever use it, is a helper pointer, you will often need one.
Whatever you are asked to do with linked lists, try it with your five hangers first,
this should help you with all programming problems you encounter.
Good luck.
I am thinking of coat hangers looking like this
\----------/
\ /
\ /
--|--
J
Not like this
---- ----
\ /
J
If you have hangers made from wire, like used by many laundry services, you can bend it a little to make a convenient spot for hooking into.
\----^-----/
\ /
\ /
--|--
J