From 16f7a0793cf707b1f7d692a4d001335c6714313a Mon Sep 17 00:00:00 2001 From: atulll <39869656+underager@users.noreply.github.com> Date: Tue, 19 Oct 2021 19:55:09 +0530 Subject: [PATCH] fix(JavaScript): doubly link list (#598) --- .../JavaScript/src/linked-lists/doubly.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/algorithms/JavaScript/src/linked-lists/doubly.js b/algorithms/JavaScript/src/linked-lists/doubly.js index 53573ddf..9aea2f45 100644 --- a/algorithms/JavaScript/src/linked-lists/doubly.js +++ b/algorithms/JavaScript/src/linked-lists/doubly.js @@ -1,4 +1,3 @@ - function DoubleLinkedList() { const Node = function(element) { this.element = element; @@ -18,7 +17,6 @@ function DoubleLinkedList() { tail.next = node; tail = node; - length++; }; @@ -53,6 +51,7 @@ function DoubleLinkedList() { // look for out-of-bounds value if (position > -1 && position < length) { + let removedNode; let current = head; let index = 0; @@ -60,14 +59,23 @@ function DoubleLinkedList() { current = current.next; index++; } + if (current.next) { + removedNode = current.next; + } if (current.next.next !== null) { current.next.next.prev = current; } current.next = current.next.next; + // if current.next is null, that means it + // is the last elem, so let's mark it as tail. + if (!current.next) { + tail = current; + } + length--; - return current.element; + return removedNode.element; } else { return null; } @@ -143,7 +151,6 @@ function DoubleLinkedList() { }; } - const newDoubleLinkedList = new DoubleLinkedList(); newDoubleLinkedList.append(1); newDoubleLinkedList.append(5); @@ -156,4 +163,5 @@ console.log(newDoubleLinkedList.toArray()); console.log(newDoubleLinkedList.toString()); newDoubleLinkedList.delete(20); -console.log('After delete 20: '+newDoubleLinkedList.toString()); +console.log('After delete 20: ' + newDoubleLinkedList.toString()); +console.log(`Tail points to : ${newDoubleLinkedList.getTail().element}`);