I want to write a function to add nodes to the end of my linked list. My problem is that I don't know how I can have a pointer that always points to the first node of my linked list.Forexample if I write

Gnode*p = new Gnode;

when I add the first node it works but for adding more than one it causes problem.

8 Years
Discussion Span
Last Post by hg_fs2002

If you want to add to the end of the list you need to either keep track of the end of the list using a dedicated node called something like tail or end or whatever, or you have to find the end of the list by looping through the list until you find the node where link is NULL. Without seeing more of the implementation, it would appear reasonable to assume that adding at first->link every time would be the equivalent of always adding at the second position of the list rather than at the end of the list.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.