Thread: singly linked list

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    4

    singly linked list

    I'm currently trying to understand singly linked lists and pointers. Can somebody write more detailed inline comments in the following code??

    Thanks in advance.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    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;   
        root->x = 12;
        conductor = root; 
        if ( conductor != 0 ) {
            while ( conductor->next != 0)
            {
                conductor = conductor->next;
            }
        }
        /* Creates a node at the end of the list */
        conductor->next = malloc( sizeof(struct node) );  
    
        conductor = conductor->next; 
    
        if ( conductor == 0 )
        {
            printf( "Out of memory" );
            return 0;
        }
        /* initialize the new memory */
        conductor->next = 0;         
        conductor->x = 42;
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2009
    Posts
    4
    I especially wonder why it's necessary to write conductor = root; on line 19.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by right2001 View Post
    I especially wonder why it's necessary to write conductor = root; on line 19.
    Were you planning to just leave conductor uninitialized, not pointing to anything, and try to use it? Were you planning to start somewhere other than the beginning of the list (since that's what root is, the beginning of the list).

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by right2001 View Post
    I especially wonder why it's necessary to write conductor = root; on line 19.
    If conductor=root wasn't there, being a pointer conductor would have pointed to any random location(address), creating several problems. Your rest of the code would have been meaningless. conductor=root will make conductor to point to the beginning of the list or the first node. Also you should use NULL instead of 0 in your code. It will be more meaningful to the context.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM
  2. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

Tags for this Thread