-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Comments
but doesn't i.e
|
It doesn't overwrite it as 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] |
if sorry if i'm going round in circles. |
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: In step 1, its important to note that In step 2, 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 |
@ObscureBrandon @caportil thank you both for helping me understand this. |
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 ^^ |
In the linked list, or queue implementation you have two lines like this.
I can't get my head around this.
surely
this.tail.next
gets blown away by the next line where we're settingthis.tail
The text was updated successfully, but these errors were encountered: