The function add_node
deals with copies of the pointers head
and tail
declared in main because they are passed to the function by value.
So this statement within the function
head = new_node;
changes the function parameter head
but not the original object head
declared in main.
Moreover the function does not use its parameter tail
.
And the function invokes undefined behavior in case when head
is not equal to NULL
because in this case it returns nothing.
After this call
add_node(head, tail);
the both pointers, head
and tail
, are still equal to NULL
. And dereferencing a null pointer
head->data = 2;
results in undefined behavior.
So truly speaking the function does not make a sense.
You should define one more structure as for example
struct list
{
struct node *head;
struct node *tail;
};
Then in main instead of these declarations
struct node *head = NULL, *tail = NULL;
you need to write for example
struct list lst = { .head = NULL, .tail = NULL };
and the function add_node
can be declared and defined the following way
int add_node( struct list *lst, int data )
{
struct node *new_node = malloc( sizeof( struct node ) );
int success = new_node != NULL;
if ( success )
{
new_node->data = data;
new_node->link = NULL;
if ( lst->tail != NULL )
{
lst->tail->link = new_node;
}
else
{
lst->head = new_node;
}
lst->tail = new_node;
}
return success;
}
And in main the function can be called for example like
add_node( &lst, 10 );
or
if ( !add_node( &lst, 10 ) )
{
puts( "Error: not enough memory." );
}
Here is a demonstrative program.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *link;
};
struct list
{
struct node *head;
struct node *tail;
};
int add_node( struct list *lst, int data )
{
struct node *new_node = malloc( sizeof( struct node ) );
int success = new_node != NULL;
if ( success )
{
new_node->data = data;
new_node->link = NULL;
if ( lst->tail != NULL )
{
lst->tail->link = new_node;
}
else
{
lst->head = new_node;
}
lst->tail = new_node;
}
return success;
}
int main(void)
{
struct list lst = { .head = NULL, .tail = NULL };
if ( add_node( &lst, 10 ) )
{
printf( "The head node contains %d\n", lst.head->data );
printf( "The tail node contains %d\n", lst.tail->data );
}
return 0;
}
The program output is
The head node contains 10
The tail node contains 10