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;
       first->link=p;

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

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.

oh thanks a lot,I've got the point.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.