mgechev / javascript-algorithms

💻 JavaScript implementations of computer science algorithms
https://mgechev.github.io/javascript-algorithms/
MIT License
7.83k stars 1.27k forks source link

Clarification on pop() and shift() methods #147

Closed rthangam closed 5 years ago

rthangam commented 5 years ago

Just wanted to clarify if pop() and shift() methods need to remove the node as well ? I see only either next or prev reference is changed ? If we need to remove the node i guess both needs to be removed ?

mgechev commented 5 years ago

Changing the reference let's the garbage collector delete the element.

rthangam commented 5 years ago

Thanks. But when i tried with the following code on chrome console it didn't deleted the element.

var linkedList = new LinkedList();

linkedList.push({ name: 'John', birthyear: 1981 }); linkedList.push({ name: 'Pavlo', birthyear: 2000 }); linkedList.push({ name: 'Garry', birthyear: 1989 }); linkedList.push({ name: 'Derek', birthyear: 1990 }); linkedList.push({ name: 'Ivan', birthyear: 1966 });

function printList(node){ console.log(node.data.name); }

linkedList.inorder(printList); VM919:2 John VM919:2 Pavlo VM919:2 Garry VM919:2 Derek VM919:2 Ivan

linkedList.pop() exports.Node {data: {…}, next: null, prev: e…s.Node} linkedList.inorder(printList); VM919:2 John VM919:2 Pavlo VM919:2 Garry VM919:2 Derek VM919:2 Ivan

linkedList.pop() exports.Node {data: {…}, next: null, prev: e…s.Node}

linkedList.inorder(printList); VM919:2 John VM919:2 Pavlo VM919:2 Garry VM919:2 Derek VM919:2 Ivan

Is there anything wrong in my test code or Chrome console does something different ?