With two pointers set up as how? To what do they point to? They could be completely different lists for all we know. There's not enough information given. Are P and Q pointing to the same node? Is one pointing to the node before, and the second after? I'll assume it's the latter:
Code:
/*
1. newNode->link =q
p->link = newNode
*/
Node *n, *p, *q, *list;
....
p = list;
q = list->link;
n = new Node;
n->link = q;
p->link = n;
The order of the last two lines doesn't matter in this case. However, assuming they're set up the way I've shown, only p is required. Here's how you can get around it:
Code:
n->link = p->link;
p->link = n;
P is only required though if you wish to insert before Q, assuming Q is not NULL. If Q is NULL, then P is required. If it's not, and you don't care where you're inserting, you can use Q in place of P.
Code:
n->link = q->link;
q->link = n;
For the second to occur:
2. p->link = newNode
newNode->link =q
Here, for them to be in this order, both P and Q must be there. Otherwise, as you suspect, you'll lose the tail end of the list (Q) after your assignment to P. Reversing the order of these two lines removes the need for Q, as seen above.
Sounds good anyway.
Quzah.