A little confused about the While condition in `doubly-linked-list.js` insertBefore and insertAfter
lchtao26 opened this issue · 5 comments
~/computer-science-in-javascript/src/data-structures/doubly-linked-list/doubly-linked-list.js
insertBefore(data, index) {
// ...omit other lines util here
// line 183
while ((current.next !== null) && (i < index)) {
current = current.next;
i++;
}
}
insertAfter(data, index) {
// ...omit other lines util here
// line 259
while ((current !== null) && (i < index)) {
current = current.next;
i++;
}
I am confused about the difference of while condition between line 183 and 259,
and do a test in node for the insertAfter method:
const { DoublyLinkedListi } = require( "./doubly-linked-list.js");
const doublyLinkedList = new DoublyLinkedList();
doublyLinkedList.add(1);
doublyLinkedList.insertAfter(1, 1);
and it will throw an the error below, it's not a expected error throw by the doubly Linked List class.
current.next.previous = newNode;
^
TypeError: Cannot read property 'next' of null
So I think current.next !== null
in insertBefore method is right for insertAfter, instead of current !== null
In your function call doublyLinkedList.insertAfter(1, 1);
; the second 1
is index. Index 1 doesn't exist just after one add()
.
const { DoublyLinkedList } = require( "./doubly-linked-list.js");
const doublyLinkedList = new DoublyLinkedList();
doublyLinkedList.add(1);
doublyLinkedList.add(2);
doublyLinkedList.add(3);
doublyLinkedList.insertAfter(10, 1);
for(let i of doublyLinkedList.values())
console.log(i)
// 1, 2, 10, 3
It is, the index 1 doesn't exist. I do it on purpose. My point is, if the index is exceed, the error should be throw by insertAfter
function as:
// line 270
if (i < index) {
throw new RangeError(`Index ${index} does not exist in the list.`);
}
and should not be throw by the the exception.
current.next.previous = newNode;
^
TypeError: Cannot read property 'next' of null
I think that is caused by the while condition current !== null
~/computer-science-in-javascript/src/data-structures/doubly-linked-list/doubly-linked-list.js
insertBefore(data, index) { // ...omit other lines util here // line 183 while ((current.next !== null) && (i < index)) { current = current.next; i++; } }
insertAfter(data, index) { // ...omit other lines util here // line 259 while ((current !== null) && (i < index)) { current = current.next; i++; }
I am confused about the difference of while condition between line 183 and 259,
and do a test in node for the insertAfter method:
const { DoublyLinkedListi } = require( "./doubly-linked-list.js"); const doublyLinkedList = new DoublyLinkedList(); doublyLinkedList.add(1); doublyLinkedList.insertAfter(1, 1);
and it will throw an the error below, it's not a expected error throw by the doubly Linked List class.
current.next.previous = newNode; ^ TypeError: Cannot read property 'next' of null
So I think
current.next !== null
in insertBefore method is right for insertAfter, instead ofcurrent !== null
Can you assign this to me