Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Linked list implementation #33

Open
magicspon opened this issue Jan 4, 2023 · 7 comments
Open

Question: Linked list implementation #33

magicspon opened this issue Jan 4, 2023 · 7 comments

Comments

@magicspon
Copy link

magicspon commented Jan 4, 2023

In the linked list, or queue implementation you have two lines like this.

this.tail.next = node
this.tail = node

I can't get my head around this.

surely this.tail.next gets blown away by the next line where we're setting this.tail

@magicspon magicspon changed the title Linked list question Question: Linked list question Jan 4, 2023
@magicspon magicspon changed the title Question: Linked list question Question: Linked list implementation Jan 4, 2023
@ObscureBrandon
Copy link

To append newly created nodes in linked lists, we have to point our current tail to the newly created node and then set the tail to our new node. The order is important as setting the tail prematurely will mean that we no longer have a way to connect the last node of our list to our new tail.

msedge_zF2v1zhHgm

@magicspon
Copy link
Author

magicspon commented Jan 4, 2023

but doesn't setting the tail to our new node overwrite this.tail including this.tail.next we set on the previous line.
I understand the why, but not the how. in my mind, line 1 is overwritten by line 2...

i.e

this.tail.next = node // --> this.tail.next === node
this.tail = node // --> now this.tail === node so -> this.tail.next === waa?

@ObscureBrandon
Copy link

ObscureBrandon commented Jan 4, 2023

It doesn't overwrite it as this.tail just points to a Node object and changing what it points to doesn't tamper with the referenced node.
Let's call our old and new tails A and B respectively.

this.tail.next = node; // sets referenced node's (node A) `next` property to our new node (node B)
this.tail = node; // instead of having `tail` point to node A we just change it to node B. This does not affect Node A
let A = [1, 2, 3];
let pointer = A;
pointer.push(4);

console.log(A); // [1, 2, 3, 4]
pointer = undefined;
console.log(A); // [1, 2, 3, 4]

@magicspon
Copy link
Author

if this.tail points to Node B... how does this.tail.next still point to Node A when on line two we are setting this.tail.
If, for example i set this.tail to 25, this.tail.next is no longer going to be a thing. what am i missing here?!!

sorry if i'm going round in circles.

@caportil
Copy link

caportil commented Jan 9, 2023

I think the confusion here has to do with the concept of Javascript references, which can be easier to understand by visualizing what's happening step by step.

Let's say you have a simple linked list below with nodes A/B/C and want to append node D. The steps you listed will execute as follows:

image

In step 1, its important to note that this.tail is just a reference to node (C), rather than being a node itself. As with the earlier example, if you create a Javascript object (i.e. let A = [1, 2, 3]) and then assign it to another variable (i.e. let B = A), the latter doesn't create a brand new copy- it just references the existing one. Similarly here, this.tail is not its its own copy of whatever node (C) was added to this linked list at some point, its just a reference to it.

In step 2, this.tail = node doesn't mutate or overwrite any data to node (C) the way step 1 does. Instead, it simply reassigns the this.tail reference from node (C) to the new node (D). This means that node (C) is untouched, which means it keeps the modifications from step 1 (its next property still points to D). But now, this.tail points to (D), as desired.

JS references (similar but not identical to the concept of "pointers") can be tricky to understand at first, but there are some great online resources that explain things pretty effectively. Personally I'd recommend the following post on Codeburst which I think does a good job of summarizing: https://codeburst.io/explaining-value-vs-reference-in-javascript-647a975e12a0

@magicspon
Copy link
Author

@ObscureBrandon @caportil thank you both for helping me understand this.

@ThePrimeagen
Copy link
Owner

i must of made myself a good ol fashion mistake while doing this live :)

I am very sorry, but hey! this is a really nice explanation of what is going on. I am unsure what to do with this other than leave it open as people need to see it if they run into this ^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants