jasonsjones / doubly-linked-list

Javascript implementation of a doubly linked list data structure.
Other
30 stars 6 forks source link

`remove(node)`, `addPrevious(node, nodeData)`, `addNext(node, nodeData)` are missing #21

Open popovitsj opened 2 years ago

popovitsj commented 2 years ago

Hi,

I was considering to use this library, but it seems to miss three very important methods on its API, namely a way to insert or remove a node not by providing the index, but by providing the node itself.

This should be constant time for a doubly linked list and is a very important feature of this datastructure when used to optimize runtime complexity in algorithms.

truszko1 commented 2 years ago

I also needed removeGivenNode and restoreGivenNode functions. I created a new class locally and extended this LinkedList with the following implementation:

        removeGivenNode: function (node) {
            node.prev.next = node.next;
            node.next.prev = node.prev;
        },

        restoreGivenNode: function (node) {
            node.prev.next = node;
            node.next.prev = node;
        },

This removes given node in constant time. If you hold onto the node, you can also put it back into the list.

Hope this helps!