-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashTableApp.java
65 lines (63 loc) · 1.7 KB
/
HashTableApp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package DataItem;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class HashTableApp {
public static void main(String agrs[])throws IOException{
DataItem aDataItem;
int akey,size,n,keyPerCell;
System.out.print("Enter size of hash table");
size=getInt();
System.out.print("Enter initial number of items:");
n=getInt();
keyPerCell=10;
HashTable thetable= new HashTable(size);
for(int j=0;j<n;j++){
akey=(int)(java.lang.Math.random()*keyPerCell*size);
aDataItem = new DataItem(akey);
thetable.insert(aDataItem);
}
while(true){
System.out.print("Enter first leterr of show,insert,delete,of find:");
char choice=getChar();
switch(choice){
case's':
thetable.displayTable();
break;
case'i':
System.out.println("Enter key value to inser:");
akey=getInt();
aDataItem= new DataItem(akey);
break;
case'd':
System.out.println("Enter key value to find");
akey=getInt();
thetable.delete(akey);
break;
case'f':
System.out.println("Enter key to find:");
akey=getInt();
aDataItem= thetable.find(akey);
if(aDataItem!=null){
System.out.println("Found"+akey);
}else{
System.out.println("Counld nt find"+akey);
}
break;
default:
System.out.print("Invalid entry\n");
}
}
}
public static String getString() throws IOException{
InputStreamReader isr= new InputStreamReader(System.in);
BufferedReader br= new BufferedReader(isr);
return br.readLine();
}
public static char getChar() throws IOException{
return getString().charAt(0);
}
public static int getInt() throws NumberFormatException, IOException{
return Integer.parseInt(getString());
}
}