oreillymedia / data_structures_and_algorithms_using_javascript

768 stars 407 forks source link

chap6-4txt #17

Open zxcchen opened 7 years ago

zxcchen commented 7 years ago

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 commented 6 years ago

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