A single mistake in add_to_list()
function:
if ((*list)->next == NULL) { // checks next of first is NULL not list is NULL
// to insert at first
should be just:
if ((*list) == NULL){ // You need to check list is NULL
Check working code
Why you were getting only
15
the last node (temp3
) value?
Because in main you creates three temp nodes and initialized next of every node to NULL, including list
node so in add_to_list()
function if condition ((*list)->next == NULL)
always evaluates true and list
always initialized with temp node.