/* Closed Hash table for storing bank accounts */ class account { String owner; // owner of bank account int ssn; // social security number double balance; // current balance; String address; // other info ... static double interest_rate = 0.01; void withdraw(double amt) { balance -= amt; } void deposit(double amt) { balance += amt; } public account(String o, int s) // constructor { owner=o; ssn=s; balance = 100; } } // class account // using the sum of the ascii values of public class closehash { account[] Table; boolean[] occupied; int size = 0; // number of current entries in table public closehash(int n) // constructor { Table = new account[n]; // default set to null occupied = new boolean[n]; // default set to false } public int hash(String key) { // compute sum of asscii values int sum = 0; for (int i=0;i Table.length) stop = true; } // while return A; } // lookup public void delete(String id) { int i = hash(id); boolean stop = false; int counter = 0; while( !stop ) { if (Table[i]==null) { if (occupied[i]) i = rehash(i); else stop=true; } else { if (Table[i].owner.equals(id)) { stop=true; Table[i] = null; size--; } else i = rehash(i); } if (++counter > Table.length) stop = true; } // while } public static void main(String[] args) throws Exception { closehash HT = new closehash(100); HT.insert(new account("mary",123896741)); HT.insert(new account("harry",761908765)); HT.insert(new account("larz",123459765)); HT.insert(new account("narx",987654321)); System.out.println(HT.lookup("larz")); HT.delete("larz"); System.out.println(HT.lookup("narx")); System.out.println(HT.lookup("larz")); } } // closehash