Linked List Tutorial sample code question

This is a discussion on Linked List Tutorial sample code question within the C Programming forums, part of the General Programming Boards category; Hello everybody, http://www.cprogramming.com/tutorial/c/lesson15.html in the sample code of that Tutorial, which shows how one can traverse a linked list, there ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Location
    Stuttgart
    Posts
    1

    Question Linked List Tutorial sample code question

    Hello everybody,

    http://www.cprogramming.com/tutorial/c/lesson15.html

    in the sample code of that Tutorial, which shows how one can traverse a linked list, there are some things I don't understand. Here is the part of the code. I've added a comment to the line I don't understand:

    Code:
    /*...*/
    struct node {
      int x;
      struct node *next;
    };
    
    int main()
    {
        /* This won't change, or we would lose the list in memory */
        struct node *root;       
        /* This will point to each node as it traverses the list */
        struct node *conductor;  
    
        root = malloc( sizeof(struct node) );  
        root->next = 0;   /* QUESTION: Why giving root->next a null pointer? */
        root->x = 12;
        conductor = root; 
        if ( conductor != 0 ) {
            while ( conductor->next != 0)
            {
                conductor = conductor->next;
            }
        }
         /*...*/
    }
    Why is root->next set to a null pointer? Isn't root->next a null pointer by default?
    Furthermore, there are 2 things I don't understand:

    1. What is the use for "if ( conductor != 0 )" when they just wrote conductor = root before?

    2. What is the goal of "while ( conductor->next != 0)"? If you just wrote "root->next = 0; conductor = root;" then is "conductor->next" in any case also 0! So the checking "while ( conductor->next != 0)" doesn't make any sense, because it's always 0.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    182
    1.
    if ( conductor != 0 )
    is a roundabout way of checking if malloc was able to allocate memory to root. If no memory was allocated malloc returns a NULL pointer address.

    2.
    The code at hand is going to be part of a more complete linked list agorithm. The conditional will check if the loop has reached the end of a linked list. As it stands now, it doesn't do anything.
    Last edited by yougene; 01-16-2009 at 10:44 AM.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by yougene View Post
    1.
    if ( conductor != 0 )
    is a roundabout way of checking if malloc was able to allocate memory to root. If no memory was allocated malloc returns a NULL pointer address.
    I agree with that - however the code is then broken, as it accesses root->next and root->x before checking that the pointer is non-null. Bad coding.

    Edit: thinking about it, it probably makes more sense with Yougene's comment #2: This comes from a bigger project, and that check is essentially "Is the list completely empty".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 01:48 PM
  3. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  4. Simple linked list question
    By netboy in forum C Programming
    Replies: 3
    Last Post: 07-26-2002, 09:08 PM
  5. linked list question
    By yeahdixon in forum C++ Programming
    Replies: 2
    Last Post: 03-28-2002, 08:16 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21