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

chap6-4txt #17

Open
zxcchen opened this issue Jun 8, 2017 · 1 comment
Open

chap6-4txt #17

zxcchen opened this issue Jun 8, 2017 · 1 comment

Comments

@zxcchen
Copy link

zxcchen commented Jun 8, 2017

function insert(newElement, item) {
   var newNode = new Node(newElement);
   var current = this.find(item);
   newNode.next = current.next;
   newNode.previous = current;
   current.next = newNode;
}```
should maintain the previous pointer of current.next,right?
@kennethakpo
Copy link

I didn't really understand your question but I can explain the above code line by line, hopefully, that helps,
the insert function needs to add a node with 'newElement' after the node that contains 'item'
-A newNode is created with newElement
-Current is a variable that holds the node with element called 'item'
-The newly created node, 'newNode' will have it's next property point to the Node that is in-front of the current node hence (newNode.next = current.next)
-The newNode's previous pointer will point to the current node
-The current note next pointer will now point to the newNode
This ensures that newNode inserts itself in-front of the node with element item or current
Hope this helps

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

2 participants