1

I was sitting in my car thinking about my LinkedList project when a question came up in my head regarding this particular code.

Node* current = head;
while (current != nullptr) {
    // do stuff here
    current = current->next; // -> Why does this not permanently affect the linkedlist?
}

In the comments I specify this. Why is setting current = current->next not permanent, yet when I add to the end of the Node the result is?

3
  • The variable current is not part of the linked list. You create it simply to keep track of the position. Commented Nov 20, 2015 at 11:12
  • You can have any number of pointers all pointing to the same memory. Changing a single pointer will not change the memory it's pointing to, just make it point somewhere else. Commented Nov 20, 2015 at 11:18
  • When working with pointers and when a bit confused, its usually a good idea to draw out a representation using blocks and arrows. For example search for "linked list" images on google Commented Nov 20, 2015 at 11:24

3 Answers 3

3

You need to look at what you are actually assigning to.

You have a local variable current. Changing that to point to different nodes doesn't modify the nodes itself. It's like you have a bit of paper with different house addresses on it. Changing the address on the paper doesn't move the houses around.

When you add a new node you look at the address on the paper, go to that house, and then knock a new door in the wall of the house. current->next = X knocks the new door through. current=X changes the address written on your bit of paper but doesn't change the house.

Sign up to request clarification or add additional context in comments.

6 Comments

I understand that; but how come when you finally get to the end and add a new node to the Node all the other pointers get affected by it? In the code above: Node* current = head What happens is when you get to the end and you add a new Node it affects "head" as well.
@Xari No, it doesn't affect all the other pointers. That's in fact a very handy property of linked lists: appending a node to the end changes only the (former) last node.
If current only keeps track of the position then, how come when you change the nullptr in the end to a new Node the node previously now points to the new node? Wouldn't that mean when you're iterating through the linkedlists each node would be modified? I know that they don't, but what confuses me is that in the end you can change the nullptr to a new Node, thereby affecting the linkedlist, but when you use current = current->next that doesn't affect anything. Though when you do current = new Node all of a sudden the previous node can point to this.
@Xari you don't write current = new Node to add a new node; you write current->next = new Node. current-> is a dereference, so you're modifying the pointer in the last node with current->next.
What @Simple said. You follow the address to the house. You then modify the house at that address.
|
0

In this piece of code you are just iterating through the link list. you are just assigning the address of link list elements to current pointer. At end of this loop current will point to nullptr.

1 Comment

I understand that; but how come when you finally get to the end and add a new node to the Node all the other pointers get affected by it? In the code above: Node* current = head What happens is when you get to the end and you add a new Node it affects "head" as well.
0

Its simple as this. Current is a pointer to the linked list and will keep changing until you don't reach to the end of the linked list according to this code. I hope this is what you are asking in your question and I have made myself clear.

3 Comments

I understand that; but how come when you finally get to the end and add a new node to the Node all the other pointers get affected by it? In the code above: Node* current = head What happens is when you get to the end and you add a new Node it affects "head" as well.
When you insert an element at the end, head is not affected. Head contains the adress of the first element, that element contains the address of the next element and so on, every element knows the next (and the previous when its a doubly linked list), but they know nothing beyond that. That is why random access is expensive as you have to run through the whole list to find a specific element, even when you know its place (index) in the list (they are not contiguous in memory)
@Xari: When we reach to the end node and add a new node, we modify the node explicitly telling the compiler that we now want to create a new linked list node and add it at the end of this linked list. On the contrast what we are doing in above code is just telling the pointer 'current' is to point to where the current->next is pointing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.