-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.java
More file actions
62 lines (53 loc) · 1.87 KB
/
Copy pathHashTable.java
File metadata and controls
62 lines (53 loc) · 1.87 KB
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
public class HashTable {
private final int Size;
private PhoneList [] Table;
HashTable(int Size) {
this.Size = Size;
Table = new PhoneList[Size];
for (int i = 0; i < Size; i++) {
Table[i] = new PhoneList();
}
}
void Insert(String name, String phone) {
int hashIndex = (int)calc_hash(name, Size);
if (Table[hashIndex].searchContact(name) == true && Table[hashIndex].getContact(name) == phone) {
System.out.println("Contact exists");
}
else Table[hashIndex].addContact(name, phone);
}
void Search(String name) {
int hashIndex = (int)calc_hash(name, Size);
if (Table[hashIndex].searchContact(name) == true) {
System.out.println("Contact found: Name: " + name + ", Phone: " + Table[hashIndex].getContact(name));
}
else System.out.println("Contact not found");
}
void Delete(String name) {
int hashIndex = (int)calc_hash(name, Size);
Table[hashIndex].deleteContact(name);
}
void update(String name, String new_phone_number){
int hashIndex = (int)calc_hash(name, Size);
Table[hashIndex].updateContact(name, new_phone_number);
}
void Display(){
for (int i = 0; i < Size; i++) {
if (Table[i] != null && !Table[i].isEmpty()) {
System.out.println("index no." + i + " : ");
Table[i].displayList();
}
}
}
public static long calc_hash(String key, int table_size) {
int i, l = key.length();
long hash = 0;
for (i = 0; i < l; i++) {
hash += Character.getNumericValue(key.charAt(i));
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
if (hash > 0) return hash % table_size;
else return -hash % table_size;
}
}