Linked List Help

Hello,

I'm working from this example:
http://www.cprogramming.com/tutorial/lesson15.html

However, I'm not sure how this works;
1
2
3
4
 if ( conductor != 0 ) {
    while ( conductor->next != 0)
      conductor = conductor->next;
  }


Bit mindbending this bit. Uuh, yeah -- how does it work?
closed account (3qX21hU5)
1
2
3
4
5
6
7
// Means while conductor isn't 0 run the if statement
if (conductor != 0) {
    // Checks weather the next node in the list is pointing to something
    while (conductor->next != 0)
        // If the next node is pointing to something it tells the conductor to point to the next node
        conductor = conductor->next;
}
Last edited on
It's simply traversing through the list.

Next is a pointer to the next node in the list. If next isn't NULL (or 0), which signifies the end of the list, the conductor (also a pointer to a node) is set to whichever node is held in next.
Okay so conductor != 0 references which part of the code however? Next I perfectly understand as NULL signifies the end of the list, but conductor != 0 is where I get confused.
In this example, 0 is synonymous with NULL.
So it basically means if conductor is not pointing to a null pointer?
Yeah, it's first checking to make sure that conductor isn't null. Then it keeps jumping to the next linked node, providing that the next node isn't null as that would be the end of the list.

Maybe this quick diagram will help...
http://s15.postimage.org/cwsrxjpyz/linkedlist.png

Edit: Forgot to denote in the diagram, but it goes without saying that root and conductor are both pointers.
Last edited on
Thanks man, that helps.

1
2
3
4
5
6
  node *root;       // This won't change, or we would lose the list in memory
  node *conductor;  // This will point to each node as it traverses the list

  root = new node;  // Sets it to actually point to something
  root->next = 0;   //  Otherwise it would not work well
  root->x = 12;


So this right here... focusing on the root->next=0; does it set the next node to 0 or the first node to 0?
I tell ya what would be useful... if someone could expand on the code provided here:
http://www.cprogramming.com/tutorial/lesson15.html

...and make a new value within the linked list as well as the one used in the example in the link above.

That would be really useful because I'll know how to add new things and how to close the linked list.

EDIT: Please :D.
Last edited on
Topic archived. No new replies allowed.