s
is null after every insertion because your code s = newnode
is setting the local s
pointer to a new value, which does not updated the contents of the original pointer in main()
. You could either have insertnode()
return the s
pointer (e.g., s = insertnode(s,5)
, not recommended), or have main()
create a sentinel node as a beginning-of-list placeholder that you then pass to insertnode()
on the first call to it (recommended).
0
solved Inserting at the end of linked list. Why S node becoming null at each insertion