737 views
0 votes
0 votes

say I have a single linked list of 6 elements and I do some operation like

temp->link->link->link->link=p , Now assume that p is a pointer holding the address of third node whose data part is 3 and link part is pointing to 4 th node , and let's assume that it has the base address of 300 .

Now assume that this entire LHS operation evaluates to the base address of 600 which is of the 6th node ,now my confusion is that after this expression evaluates ,scene would be like 

600 = p ,so now how come through this expression the link part of the 6th node starts pointing to the address held by the pointer variable p ,i.e. how come this link points to 3rd variable ,plz clarify this . 

2 Answers

0 votes
0 votes
if the LHS evaluate to the 600 which is the address of 6th node then it is in 5th node . this means the next points always points to the next node and while traversing the next part u got 600 then it must be in the next part of the previous node and the node which will be containing it will be the 5th node if continuous . if the next node is pointing to 600 u just replace with 300 . now there will be a loop in the link list the 6th node will be disconnected and the link list will be like . 0-1-2-3-4-5-3-4-5-3.... no ending will be there.
0 votes
0 votes

You could have asked the question in an easier way :)

Now to explain more I assume C language and sizeof int 4 and size of pointer 8. 

  Node 1 Node 2 Node 3 Node 4 Node 5
  Address Value Address Value Address Value Address Value Address Value
data (int) 1024  1 2000 2 0214 3 5000 4 0600 5
link (*) 1028 2000 2004 0214 0218 5000 5004 0600 0604 0 (null)

Now, as per the question we have 

0600 = p. 

And this is works just like a normal assignment but changes the structure of the list as shown in bottom table. 

This is actually not correct. we'll have something like 

node5->next = p;

because in C language first operand of "=" operator must be an lvalue- 600 is not an lvalue but a constant. An lvalue can be a non-const variable or a pointer but never a constant. 

Now, it doesn't end here. I suppose no one will think above this during B.Tech. unless one makes a compiler himself. For this reason Prof. Srikant Sir in IISc. still makes everyone of his course students make a compiler himself for taking compiler course in IISc. 

The thing to the last statement is p has an address as well as value. By definition of "=" in C language, the value of p is copied to node5->next and not address. i.e., the value of right operand (rvalue) is copied to the address of left operand (lvalue)- making it a necessity that left operand must have an address. This is all there to l and rvalues. 

Now p has the value 0214 (third node address as given in question). So, the final list will be 

  Node 1 Node 2 Node 3 Node 4 Node 5
  Address Value Address Value Address Value Address Value Address Value
data (int) 1024  1 2000 2 0214 3 5000 4 0600 5
link (*) 1028 2000 2004 0214 0218 5000 5004 0600 0604 0214
 

Making a loop inside the list. Now the list looks like a smoking pipe as shown below (assume the bend is complete).

Related questions

1 votes
1 votes
1 answer
2
Imarati Gupta asked Jan 1, 2017
13,574 views
The order in which operands are evaluated in an expression is predictable if the operator isa) * b) + c) % d) &&
0 votes
0 votes
1 answer
4