Skip to content

Small code errors for the LinkedList and HashTable data structures #1033

Open
@AlexIoanGheorghita

Description

@AlexIoanGheorghita

Hello!

As I went through the code for these data structures, I noticed two small errors:

  1. inside the delete() method of the LinkedList data structure, there is a line of code that says: while (this.head && this.compare.equal(this.head.value, value)). However, instead of a while it should be an if statement.

  2. inside the constructor() of the HashTable data structure, we initialize an array in the following way: this.buckets = Array(hashTableSize).fill(null).map(() => new LinkedList());. However, the Array() constructor should have the new keyword in front of it.

If you guys feel that these are important fixes to you and if you allow me to help, I would be more than glad to contribute by creating a pull request with the changes made.

Activity

mithleshgupta

mithleshgupta commented on May 21, 2023

@mithleshgupta

I can do this , can you assign this to me

Saminakalwar

Saminakalwar commented on Jun 28, 2023

@Saminakalwar

Hello ..Its SSOC Contributor .I can easily do this .
.Can u assign me issue under SSOC'23 label please ?

sujitmahapatra

sujitmahapatra commented on Oct 7, 2023

@sujitmahapatra

For the LinkedList data structure, you're correct that the while statement should be replaced with an if statement. Here's the corrected delete method:

delete(value) {
    if (!this.head) {
        return;
    }

    if (this.compare.equal(this.head.value, value)) {
        this.head = this.head.next;
        return;
    }

    let current = this.head;
    while (current.next) {
        if (this.compare.equal(current.next.value, value)) {
            current.next = current.next.next;
            return;
        }
        current = current.next;
    }
}

For the HashTable data structure, you're correct that the Array() constructor should have the new keyword. Here's the corrected constructor:

constructor(hashTableSize) {
    this.buckets = new Array(hashTableSize).fill(null).map(() => new LinkedList());
    this.keys = {};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @mithleshgupta@AlexIoanGheorghita@sujitmahapatra@Saminakalwar

      Issue actions

        Small code errors for the LinkedList and HashTable data structures · Issue #1033 · trekhleb/javascript-algorithms