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

add() function in linked-list.js #8

Closed
gabivlj opened this issue Apr 19, 2019 · 4 comments · Fixed by #9
Closed

add() function in linked-list.js #8

gabivlj opened this issue Apr 19, 2019 · 4 comments · Fixed by #9
Labels

Comments

@gabivlj
Copy link

gabivlj commented Apr 19, 2019

In the add() function in linked-list.js I see the following line of code:
if (current.next) { current.next.previous = newNode; } // <7>
What is the reason behind this line of code? If we are inserting behind current, I don't know why would you set the current.next.previous to the node we wanna insert.

https://github.com/amejiarosario/dsa.js/blob/cc1ccc0183ea5d4675815413b7d1d4cc14da348c/src/data-structures/linked-lists/linked-list.js#L83-L105

@gabivlj gabivlj changed the title A question with linked-list.js add() function in linked-list.js Apr 19, 2019
@amejiarosario
Copy link
Owner

amejiarosario commented Apr 19, 2019

The short answer is... You need to update current.next.previous because it needs to point to the newly created node. When you add a new element to the middle of the list you need to update the 4 references around it and this is one of them.


The long answer is...

I'll use the example on the docs to explain this line better.

Let's say we have the following linked list:

art -> dog -> cat

and we want to insert a node called "new" on the position 1 (middle of the list).

https://github.com/amejiarosario/dsa.js/blob/cc1ccc0183ea5d4675815413b7d1d4cc14da348c/src/data-structures/linked-lists/linked-list.js#L92

So, current would be dog node, since is the one currently on position 1.

After we create the "new" element, to add it to the middle of the list we need to update 4 links

  1. new.previous
  2. new.next
  3. art.next
  4. dog.previous

image

image

In code, it looks like this:
https://github.com/amejiarosario/dsa.js/blob/cc1ccc0183ea5d4675815413b7d1d4cc14da348c/src/data-structures/linked-lists/linked-list.js#L92-L99

So after we create the node, we need to update 4 references:

  1. Update node link new.previous: https://github.com/amejiarosario/dsa.js/blob/cc1ccc0183ea5d4675815413b7d1d4cc14da348c/src/data-structures/linked-lists/linked-list.js#L95

  2. Update node link new.next: https://github.com/amejiarosario/dsa.js/blob/cc1ccc0183ea5d4675815413b7d1d4cc14da348c/src/data-structures/linked-lists/linked-list.js#L96

  3. Update node link art.next: https://github.com/amejiarosario/dsa.js/blob/cc1ccc0183ea5d4675815413b7d1d4cc14da348c/src/data-structures/linked-lists/linked-list.js#L98

  4. Update node link dog.previous: https://github.com/amejiarosario/dsa.js/blob/cc1ccc0183ea5d4675815413b7d1d4cc14da348c/src/data-structures/linked-lists/linked-list.js#L99

@amejiarosario
Copy link
Owner

amejiarosario commented Apr 19, 2019

@gabivlj Let me know if that explanation makes sense or if you still have doubts. I might add it to the main docs if this helps to understand.

@gabivlj
Copy link
Author

gabivlj commented Apr 19, 2019

Shouldn’t it be current.previous = newNode instead of current.next.previous = newNode?

@amejiarosario
Copy link
Owner

amejiarosario commented Apr 19, 2019

@gabivlj You are totally right! It should be current.previous = newNode. I'll update the code/docs/tests. Thanks for reporting this!

amejiarosario added a commit that referenced this issue Apr 19, 2019
When inserting an item on the middle of a linked list one reference was not being updated properly.

Fixed #8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants