Comments (5)
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
from computer-science-in-javascript.
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
from computer-science-in-javascript.
~/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
from computer-science-in-javascript.
Can you assign this to me
from computer-science-in-javascript.
Related Issues (18)
- javascript HOT 1
- bubbleSort misprint
- This should have an npm package. HOT 3
- For quick sort program
- es6 conversion HOT 2
- Question about Base64Decode HOT 1
- Why is the LinkedList constructor necessary? HOT 1
- Merge binary trees
- bad header in quicksort
- Incorrect removal in BST?
- No merge sort HOT 1
- merge-sort-inplace.js is not In Place
- base64
- Professional JavaScript for Web Developers(Fourth Edition)Chinese????
- Exponentiation by squaring
- Compilation error HOT 1
- Removing a node with two children messes up the structure HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from computer-science-in-javascript.