Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A little confused about the While condition in doubly-linked-list.js insertBefore and insertAfter #51

Open
lchtao26 opened this issue Feb 8, 2021 · 5 comments

Comments

@lchtao26
Copy link

lchtao26 commented Feb 8, 2021

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

@lchtao26 lchtao26 changed the title hile condition in doubly-linked-list.js ome confuse about the While condition in doubly-linked-list.js insertBefore and insertAfter Feb 8, 2021
@lchtao26 lchtao26 changed the title ome confuse about the While condition in doubly-linked-list.js insertBefore and insertAfter A little confused about the While condition in doubly-linked-list.js insertBefore and insertAfter Feb 8, 2021
@IAmAnubhavSaini
Copy link

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

@lchtao26
Copy link
Author

lchtao26 commented Feb 9, 2021

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

@mikro03
Copy link

mikro03 commented Aug 26, 2022

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

@mithleshgupta
Copy link

Can you assign this to me

@BajiShaik07
Copy link

class Node {
constructor(data) {
this.data = data;
this.next = null;
this.prev = null;
}
}

class DoublyLinkedList {
constructor() {
this.head = null;
this.tail = null;
}

insertBefore(nodeToInsert, node) {
if (nodeToInsert === null) return;

if (node === this.head) {
  nodeToInsert.next = this.head;
  this.head.prev = nodeToInsert;
  this.head = nodeToInsert;
} else {
  nodeToInsert.prev = node.prev;
  nodeToInsert.next = node;
  node.prev.next = nodeToInsert;
  node.prev = nodeToInsert;
}

}

insertAfter(nodeToInsert, node) {
if (nodeToInsert === null) return;

if (node === this.tail) {
  nodeToInsert.prev = this.tail;
  this.tail.next = nodeToInsert;
  this.tail = nodeToInsert;
} else {
  nodeToInsert.prev = node;
  nodeToInsert.next = node.next;
  node.next.prev = nodeToInsert;
  node.next = nodeToInsert;
}

}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants