Thread: about linked lists

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    2

    about linked lists

    hi

    i just have one question regarding linked lists.

    In the cprogramming.com tutorial about simple linked lists, they show this while loop to traverse all the nodes of the linked list ( i put the printf):

    Code:
     while (conductor->next != NULL){   printf("Valor: %d\n", conductor->value);  conductor = conductor->next;  }
    my question is: Why the while condition:
    conductor->next != NULL, will work, and the conductor != NULL won't. I tried the last condition and all the data in the nodes were printed, but i got a Segmentation Fault error at the last.

    Thanks in advance..

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > conductor->next != NULL, will work, and the conductor != NULL won't.
    That would depend on how well you constructed the linked list in the first place.

    If you were sloppy about setting next pointers when you built the list, you could be having all sorts of problems.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Microsoft Lover afreedboy's Avatar
    Join Date
    Nov 2003
    Posts
    189
    conductor->next is pointer to next element in a linked list. So only next pointer found Null should be working. not like the whole conductor found Null after all.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >So only next pointer found Null should be working.
    Not necessarily. As long as you have another reference into the list (root, for example, from the tutorial) you can walk conductor off the end:
    Code:
    conductor = root;
    while ( conductor != NULL ) {
      printf ( "%d\n", conductor->value );
      conductor = conductor->next;
    }
    Provided you don't ever actually dereference the null pointer, everything is kosher. If root isn't a valid reference to the list, you will have problems though. That's why the tutorial always makes sure to create a root node before doing anything else.

    >but i got a Segmentation Fault error at the last.
    Post the code you've been using. It's quite likely that you didn't build the list properly.
    My best code is written with the delete key.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Neither of these checks will work if you have a circular list.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 05:30 PM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  3. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM