Hi,
The following code is for traversing the linked list:
node is a structure and next is a pointer to next node declared in node. start is the pointer to the first node.

node* temp = start;
while(temp != NULL)
    temp = temp->next;

I dont understand the statement temp = temp->next;, that is how does it points to the next node. I was expecting something like,temp += temp->next;
Can anyone please explain this for me.

Edited 6 Years Ago by manaila: forgot something, "start"

temp->next is a pointer to the next node... so by assigning temp to next, you're leaving the current node and going to the next node.

Hi,
The following code is for traversing the linked list:
node is a structure and next is a pointer to next node declared in node. start is the pointer to the first node.

node* temp = start;
while(temp != NULL)
    temp = temp->next;

I dont understand the statement temp = temp->next;, that is how does it points to the next node. I was expecting something like,temp += temp->next;
Can anyone please explain this for me.

After you say "node* temp = start;", temp now is simply a number that tells you how far you are from the beginning of the usable memory for your computer. Specifically, the number representing the memory address of where the list begins. So if you were to "cout << temp" at this point, you would just get some location in memory and it would say something like "0xf0000123" which is just some number in hexadecimal--the memory address of where the list begins.

When you say temp->next, internally, the computer sees the "->" and this tells it to go to that "temp" offset in memory and see what's there, and see if there are any labels called "next". So it'll find next unless you're at the end of the list, but what is "next". Just another memory offset number.

When you say temp = temp->next. Here is what you are saying (for example, lets assume the list begins at address offset 10000 in decimal for simplicity, which means you are 10000 bytes past the beginning of your usable memory. 0-9999 contains who knows... empty space, Windblows or porn downloads, or other parts of your program):

start = memory starting at 10000.
temp = memory starting at 10000. (after you set it equal to start)
temp->next = memory starting at 10010. (if for example the first node is 10 bytes long! (using 10000-10009) it could be 10050 or even say 18000 if the node contains 8000 bytes of data.)
so naturally, you want to say temp=temp->next.

If you say temp += temp->next what you are doing is adding 10000 + 10010 = 20010 so you're probably far surpassing the end of your list and who knows what's beyond it... if you start writing data to that "node", you are potentially overwriting your operating system instead with nonsense :)

If you knew the exact size of all the nodes to begin with, you wouldn't need to deal with the "next", you could do it your way, only with numbers, and say next += 10. But the nodes can vary in size so you have to rely on all those pointers to next.
-Greywolf

node *temp=start
it assigns temp the address of start....
nw temp->start points to the address contained in start's *next...

i will show u hw it luks like in terms of pointers view

struct of node is like that:

|data|*next|
node no-> 1 2 3 4
nodes->|data1|256| |data2|300| |data3|500| |data4|null|
addresses of nodes-> 200 256 300 500

so temp->next points to address of next node i.e node1's *next points to node2's address.
ok i hope nw ur doubts will be clear...

This article has been dead for over six months. Start a new discussion instead.