1

Just wondering how am I able to delete an entry from a hash table with the functionality of the insert being like this?

class HashTable{
    public int hash(int id){ return id%10;}

    private HNode[] head=new HNode[10];
    
    public  HashTable(){for(int i=0;i<10;i++)head[i]=null;}

    public  void insert(int k, String nm, int a, String g, String mOB, int c, String add, int pn)
        {      HNode temp =new HNode(k,nm,a,g,mOB,c,add,pn);
               int index=hash(k);
               temp.next=head[index];
               head[index]=temp;}
    
    
    
    public  HNode[] readHNode() {return head;}
    
    
    public  HNode search(int k)
        {     
        int index=hash(k);
        HNode temp=head[index];  
        boolean found=false;
        while(temp!=null&&found==false) {
               if (temp.key==k){found=true; break;}
               temp=temp.next;
        }
        return temp;}


}
khelwood
  • 55,782
  • 14
  • 81
  • 108
caoim
  • 13
  • 4

1 Answers1

0

Let x be the element to be removed.

In words: if x not exist, do nothing. else - attach the list before "x" (in the relevent index on "head") to the list after x.

let's say you have

 x1,x2,x3

which has the same hash value "h1".

head[h1] is a list with x1,x2,x3.

Basically, you should make the assignment:

x1.next = x3
javadev
  • 688
  • 2
  • 6
  • 17