explane the java code below: LPHTb1.java import java.util.Scanner; class LPHTbl { private int currSz, maxSz; private String[] keyzzz; private String[] values; public LPHTbl(int cap) { currSz = 0; maxSz = cap; keyzzz = new String[maxSz]; values = new String[maxSz]; } public void chckEmpty() {
can you explane the java code below:
LPHTb1.java
import java.util.Scanner;
class LPHTbl {
private int currSz, maxSz;
private String[] keyzzz;
private String[] values;
public LPHTbl(int cap) {
currSz = 0;
maxSz = cap;
keyzzz = new String[maxSz];
values = new String[maxSz];
}
public void chckEmpty() {
currSz = 0;
keyzzz = new String[maxSz];
values = new String[maxSz];
}
public int getSz() {
return currSz;
}
public boolean chckFull() {
return currSz == maxSz;
}
public boolean empty() {
return getSz() == 0;
}
public boolean cntn(String keys) {
return get(keys) != null;
}
private int hashing(String keys) {
return keys.hashCode() % maxSz;
}
public void ins(String keys, String val) {
int tempo = hashing(keys);
int uu = tempo;
do {
if (keyzzz[uu] == null) {
keyzzz[uu] = keys;
values[uu] = val;
currSz++;
return;
}
if (keyzzz[uu].equals(keys)) {
values[uu] = val;
return;
}
uu = (uu + 1) % maxSz;
} while (uu != tempo);
}
public String get(String keys) {
int uu = hashing(keys);
while (keyzzz[uu] != null) {
if (keyzzz[uu].equals(keys))
return values[uu];
uu = (uu + 1) % maxSz;
}
return null;
}
public void remove(String keys) {
if (!cntn(keys))
return;
int uu = hashing(keys);
while (!keys.equals(keyzzz[uu]))
uu = (uu + 1) % maxSz;
keyzzz[uu] = values[uu] = null;
for (uu = (uu + 1) % maxSz; keyzzz[uu] != null; uu = (uu + 1) % maxSz) {
String tmp1 = keyzzz[uu], tmp2 = values[uu];
keyzzz[uu] = values[uu] = null;
currSz--;
ins(tmp1, tmp2);
}
currSz--;
}
public void printHashTable() {
System.out.println("\nHash Table: ");
for (int uu = 0; uu < maxSz; uu++)
if (keyzzz[uu] != null)
System.out.println(keyzzz[uu] + " " + values[uu]);
System.out.println();
}
}
public class LinearProbingHashTableTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Hash Table Test\n\n");
System.out.println("Enter size");
LPHTbl tbl = new LPHTbl(sc.nextInt());
char choi = 0;
do {
System.out.println("\nHash Table Operations\n");
System.out.println("1. insert ");
System.out.println("2. remove");
System.out.println("3. get");
System.out.println("4. clear");
System.out.println("5. size");
int ch = sc.nextInt();
switch (ch) {
case 1:
System.out.println("Enter keys and value");
tbl.ins(sc.next(), sc.next());
break;
case 2:
System.out.println("Enter keys");
tbl.remove(sc.next());
break;
case 3:
System.out.println("Enter keys");
System.out.println("Value = " + tbl.get(sc.next()));
break;
case 4:
tbl.chckEmpty();
System.out.println("Hash Table Cleared\n");
break;
case 5:
System.out.println("Size = " + tbl.getSz());
break;
default:
System.out.println("Wrong Entry \n ");
break;
}
tbl.printHashTable();
System.out.println("\nDo you want to continue (Type y or n) \n");
ch = sc.next().charAt(0);
} while (choi != 'Y' || choi != 'y');
}
}
Step by step
Solved in 2 steps