Code:
struct node {
int x;
node *next;
};
int main()
{
node *root; // these are 2 pointers to the structure
node *conductor;
root = new node; // it uses a ptr to make a starting point
Code:
root->next = 0; // Need more explanation
That's the same as (*root).next = 0. What it's doing is setting the "next" member of the newly allocated structure assigned to root to 0, or NULL -- the standard value used to indicate that the pointer is in fact pointing at nothing at all.
Code:
root->x = 12; // Need more explanation
This is setting the x value of the first node in the list, the element that was dynamically allocated and assigned to root. It's syntactically equivalent to (*root).x = 12;.
Code:
conductor = root; // sets the other ptr = the ptr conductor
Code:
if ( conductor != 0 ) { // if the conductor not = to 0 ... how does it know that? never defined?
What do you mean? conductor was "defined" -- it's a variable of type node*.
Basically that's checking to see if conductor is pointing at a valid element or not. If it's not, conductor will contain 0 or NULL. Presumably this is so the contents of conductor can be used. Accessing a node that doesn't really exist is not a very good idea.
Code:
while ( conductor->next != 0) // Need more explanation
conductor = conductor->next; // Need more explanation
This loop loops while the next element of the structure that conductor is pointing to is a valid node. Every time, it advances conductor to point to the next node.
In effect, after this loop executes, conductor will be pointing at the last element in the linked list, because conductor->next will be NULL -- there will be no node after the current one.
Code:
}
conductor->next = new node; // Need more explanation
conductor->next, which was NULL, is now being assigned to a new, dynamically allocated node.
Code:
conductor = conductor->next; // Need more explanation
Just like in the loop, conductor is advanced to point to the new node.
Code:
conductor->next = 0; // Need more explanation
conductor->x = 42; // Need more explanation
}
42! The answer to life, the universe, and everything! Never mind.
The data in the new node is being set up. Before this happens, the node will contain random data, so setting it is a good idea.