Thread: linked tree

  1. #1
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528

    linked list

    I am going through a tutorial on linked lists and I am having trouble in telling what some piece of code achieves.
    This are the structures used for constructing the list
    Code:
     struct jsw_node {
           void*data;
           struct jsw_node *next;
    };
    struct jsw_list {
            struct jsw_node *head;
            int has_dummy_head; 
            size_t size;
    };
    
    The comes this function that serves to create a new list with an optional dummy head
    Code:
    struct jsw_list *new_list ( int has_dummy_head ){
          struct jsw_list *rv = malloc ( sizeof*rv );
          if ( rv != NULL )
                {   rv->head = has_dummy_head ? new_node ( NULL, NULL ) : NULL; 
                    rv->has_dummy_head = has_dummy_head; 
                       rv->size =0; 
                  if ( has_dummy_head && rv->head == NULL ) {
                     /* Release the list if a dummy couldn't be allocated */  
                      free ( rv );   
                        rv = NULL;   
                        } 
                   }
                return rv;
    }
    There is a new_node function that create a new node given the data and the links
    Code:
    struct jsw_node * new_node(void * data,struct jsw_node * next/*this is the node to link to*/){
      struct jsw_node * rv=malloc(sizeof *rv);
      if(rv!=NULL){
        rv->data=data;
        rv->next=next;
      }
      return rv;
    }
    what does this part of the function new_list achieve?
    Code:
     rv->head = has_dummy_head ? new_node ( NULL, NULL ) : NULL;
    Last edited by Aslaville; 06-29-2013 at 12:49 AM.

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by Aslaville View Post
    what does this part of the function new_list achieve?
    Code:
     rv->head = has_dummy_head ? new_node ( NULL, NULL ) : NULL;
    If has_dummy_head is true, then rv->head is set to a node allocated by new_node(), otherwise it's set to NULL.

  3. #3
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    I had also thought so but imagined the data member has_dummy_node should be a bool value only to realize this is C and no C++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tree of Struct with Linked List
    By jjeanie in forum C Programming
    Replies: 4
    Last Post: 05-03-2007, 06:36 PM
  2. Binary Tree -vs- Linked List
    By dbnelson2403 in forum C Programming
    Replies: 7
    Last Post: 02-08-2006, 08:59 AM
  3. Linked List Tree
    By Krak in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2005, 12:03 PM
  4. Linked List To Binary Tree
    By DaJoe in forum C Programming
    Replies: 3
    Last Post: 05-12-2002, 10:59 PM
  5. Help: One Question for each: Linked List and Tree
    By Yin in forum C++ Programming
    Replies: 1
    Last Post: 04-14-2002, 12:57 AM