someone please tell me, why is this not working. why is the L1 not being updated by addbegin() function. literally have been stuck in 2 programs for last 4 hours due to this reason.
@cire sorry. i didn't get that. In "addend()", according to your logic, I am taking a copy of 'L' and and by the help of 'T' pointer making all the modification of adding new nodes at end to that copy of L. but then, why is the same being reflected in main() while using "printL(L)" where argument 'L' is the original 'L'.
i didn't get that. In "addend()", according to your logic, I am taking a copy of 'L'
Yes. What you are not doing is modifying that copy and expecting the original to be affected. In other words, you don't assign to L as you did in addstart.
and by the help of 'T' pointer making all the modification of adding new nodes at end to that copy of L.
T is not needed.
// Equivalent to what you've written:
1 2 3 4 5 6 7 8 9
void addend(node * L,int d)
{
while ( L->next ) L = L-> next;
L->next = new node;
L = L->next;
L->data = d;
L->next = nullptr;
}
And note that the original in main is not somehow magically pointing to the last node in the list. It still points to the first.
while using "printL(L)" where argument 'L' is the original 'L'.
printL accepts a pointer by value, therefore it operates on a copy of the original, and you'll notice that my version of printL takes advantage of that.