Giter VIP home page Giter VIP logo

Comments (5)

IAmAnubhavSaini avatar IAmAnubhavSaini commented on June 16, 2024

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.

lchtao26 avatar lchtao26 commented on June 16, 2024

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.

mikro03 avatar mikro03 commented on June 16, 2024

~/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

from computer-science-in-javascript.

mithleshgupta avatar mithleshgupta commented on June 16, 2024

Can you assign this to me

from computer-science-in-javascript.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.