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;