NewbieWizzy
NewbieWizzy

Reputation: 7

How to delete a node in HashTable in Java?

I'm a newbie in programming. I am currently learning Java Programming Language. My problem is, I am trying to delete a specific node that contains a value in HashTable but I can't figure it out where did I go wrong. Can someone explains to me on how to delete this specific node? Thank you in advance. Sorry for my bad English.

Output that I want to aim: "Albert" and "Timmy" should be deleted.

This is my code:

Class: HashEntry

public class HashEntry {
    private int key;
    private String value;
    private HashEntry next;

    public HashEntry() {
        this.next = null;
    }

    public HashEntry(int key, String value) {
        this.key = key;
        this.value = value;
        this.next = null;
    }

    public int getKey() {
        return key;
    }

    public String getValue() {
        return value;
    }

    public void setNext(HashEntry next) {
        this.next = next;
    }

    public HashEntry getNext() {
        return this.next;
    }
}

Class: HashTableArray

public class HashTableArray {
    HashEntry[] arrayHash;
    int size;

    public HashTableArray(int size) {
        this.size = size;
        arrayHash = new HashEntry[size];
    }

    public int getHash(int key) {
        return key % size;
    } 

    public void insert(int key, String value){
        int hashInd = getHash(key);
        HashEntry newVal = new HashEntry(key, value);
        if (arrayHash[hashInd] == null) {
            arrayHash[hashInd] = newVal;
        }
        else {
            HashEntry arrayValue = arrayHash[hashInd];
            while (arrayValue.getNext() != null) {
                arrayValue = arrayValue.getNext();
            }
            arrayValue.setNext(newVal);
        }
    }

    public void displayTable() {
        System.out.println("Hash Table:");
        for (int i=0; i<arrayHash.length; i++) {
            if(arrayHash[i] != null) {
                HashEntry temp = arrayHash[i];
                while(temp.getNext() != null) {
                    System.out.println(temp.getKey() + ", " + temp.getValue());
                    temp = temp.getNext();
                }
                System.out.println(temp.getKey() + ", " + temp.getValue()); 
            }
        }    
    }
    public void delete (int key, String value) {
        int hashInd = getHash(key);
        HashEntry head = arrayHash[hashInd];
        if (arrayHash[hashInd] != null) {

            HashEntry temp = head, prev = null;
            if (temp.getNext() == null && head.getValue().equalsIgnoreCase(value)) {
                head = null;
            }
            else if (temp.getNext() != null && temp.getValue().equalsIgnoreCase(value)) {
                head = temp.getNext();
            }
            else {
                while(!temp.getValue().equalsIgnoreCase(value)) {
                    prev = temp;
                    temp = temp.getNext();
                }
                if (temp == null) {
                    prev.setNext(null);
                }
                else {
                    prev.setNext(temp.getNext());   
                }
            }
        }
    }
}

Class: HashTableProgram

public class HashTableProgram {
    
    public static void main(String[] args) {
        HashTableArray h = new HashTableArray (10);

        h.insert(12, "Albert");
        h.insert(26, "Johnson");
        h.insert(5, "Timmy");
        h.insert(12, "George");

        h.displayTable();

        h.delete(12, "Albert");
        h.delete(5, "Timmy");

        System.out.println("\nAfter deleted...");
        h.displayTable();
    }
}

Upvotes: 0

Views: 299

Answers (1)

SiAce
SiAce

Reputation: 391

After a quick test, the problem lies in your delete method in HashTableArray, you should change

head = null;

head = temp.getNext();

to

arrayHash[hashInd] = null;

arrayHash[hashInd] = temp.getNext();

The final code will look like this:

public void delete(int key, String value) {
    int hashInd = getHash(key);
    HashEntry head = arrayHash[hashInd];
    if (arrayHash[hashInd] != null) {

        HashEntry temp = head, prev = null;
        if (temp.getNext() == null && head.getValue().equalsIgnoreCase(value)) {
            arrayHash[hashInd] = null;
        } else if (head.getNext() != null && head.getValue().equalsIgnoreCase(value)) {
            arrayHash[hashInd] = temp.getNext();
        } else {
            while (!temp.getValue().equalsIgnoreCase(value)) {
                prev = temp;
                temp = temp.getNext();
            }
            if (temp == null) {
                prev.setNext(null);
            } else {
                prev.setNext(temp.getNext());
            }
        }
    }
}

The problem is that when you assign arrayHash[hashInd] to head like this:

HashEntry head = arrayHash[hashInd];

You are creating a reference (or pointer) variable head who is pointing to the actual HashEntry data on the heap. If you try to set head to null, you are just changing what the head is pointing to, and now it points to null. You are not changing the actual HashEntry data in arrayHash.

Upvotes: 1

Related Questions