I have 2 codes. (that I had read in one book). the explanation under each code is true (because my book say that !!!). But in my thinking, if one code is true, so the other will be wrong. (wrong mean it will not have result same with the explanation).
First is:

p=l.pHead;
l.pHead=p->pNext; p->pNext=NULL;
//t[B][/B]his action will take p is the first node of Pointer l, after that, Pointer l will be the after value
//For example: Pointer l is ABCD
//After this action:p=ABCD; l=ABCD;
//After that: p=A<NULL> l=BCD

Second is:

l.pHead=l1.pHead;l1.pTail->pNext=X; //X is other pointer
//This action will connect  2 pointer l1 and X to new pointer l.

In my opinion, if second code is True, when you change l1 pointer (l1.pTail->pNext=X), It mean you change l Pointer, too.
So, in first code, l depend on p (l.pHead=p->pNext), so when you "change p, it mean you change l, too" So, when p.pNext=NULL, It mean

l.pHead=p.pNext;
p.pNext=NULL;
Thus, l.pHead=NULL;

so: L will be A, not ABCD.
So, Who knows what am I wrong, please answer to me, please.

thanks a lot :)

Recommended Answers

All 2 Replies

This:

l.pHead=p.pNext;
p.pNext=NULL;
Thus, l.pHead=NULL;

is wrong. You are thinking about the = operator as if it was like in mathematics. When you write "a = b" in C++ (and in almost any programming language), it means that you copy the value of b and assign it to a. After the expression has executed, there is no relationship between a and b. Changing b's value will not change a's value. It is not like a mathematical equality relation where you state "a" is equal to "b" forever and always. It's a different logic in programming, it's just execution of one line after the other.

As you say, in second code,

l.pHead=l1.pHead;l1.pTail->pNext=X;//X is another pointer

when I connect l1 with X (l1.pTail->pNext=X), It will not connect X pointer to l pointer.
So, please tell me more detail, please.
thanks :)

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.