-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTableDemo.java
More file actions
167 lines (150 loc) · 4.96 KB
/
HashTableDemo.java
File metadata and controls
167 lines (150 loc) · 4.96 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package com.yijie.hashtable;
import java.util.Locale;
import java.util.Scanner;
public class HashTableDemo {
public static void main(String[] args) {
//TEST
HashTable hashTable = new HashTable(8);
//create a menu to test the hashtable
String key = "";
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("add: add employee");
System.out.println("list: show employees");
System.out.println("search: search employee");
System.out.println("exit: quit");
key = scanner.next();
switch (key) {
case "add":
System.out.println("enter the id:");
int id = scanner.nextInt();
System.out.println("enter the name:");
String name = scanner.next();
Employee employee = new Employee(id, name);
hashTable.add(employee);
break;
case "list":
hashTable.list();
break;
case "search":
System.out.println("enter the id to be searched in hash table:");
id = scanner.nextInt();
hashTable.searchEmployeeById(id);
break;
case "exit":
scanner.close();
System.exit(0);
default:
break;
}
}
}
}
//Create the HashTable to manage the several linked lists
class HashTable {
private EmployeeLinkedList[] employeeLinkedLists;
private int size;
//constructor
public HashTable(int size) {
this.size = size;
//initialize the employeeLinkedLists
employeeLinkedLists = new EmployeeLinkedList[size];
for (int i = 0; i < size; i++) {
employeeLinkedLists[i] = new EmployeeLinkedList();
}
}
//add employees
public void add(Employee employee) {
int emloyeeLinkedListNum = hashFun(employee.id);
//add the employee to the corresponding linked list
employeeLinkedLists[emloyeeLinkedListNum].add(employee);
}
//traverse the linked list
public void list() {
for (int i = 0; i < size; i++) {
employeeLinkedLists[i].list(i);
}
}
//search the employee by id
public void searchEmployeeById(int id) {
int employeeLinkedListNum = hashFun(id);
Employee employee = employeeLinkedLists[employeeLinkedListNum].searchEmployeeById(id);
if (employee != null) {
System.out.printf("the employee is found in the linked list %d, the id = %d", employeeLinkedListNum, employee.id);
} else {
System.out.println("the employee is not found in the hash table!");
}
}
public int hashFun(int id) {
return id % size;
}
}
class Employee {
public int id;
public String name;
public Employee next;
public Employee(int id, String name) {
super();
this.id = id;
this.name = name;
}
}
//create a EmployeeLinkedList
class EmployeeLinkedList {
//head pointing to the first Employee
private Employee head;
//add Employees to the linked list
public void add(Employee employee) {
//suppose id increases with the number of employees
//when adding the first employee
if (head == null) {
head = employee;
return;
}
//when continually adding the employees after the first employee
Employee curEmployee = head;
while (true) {
if (curEmployee.next == null) {
break;
}
curEmployee = curEmployee.next;
}
//add the new employee to the end
curEmployee.next = employee;
}
//traverse the employee
public void list(int num) {
if (head == null) {
System.out.println("the linked list " + num + " is empty!");
return;
}
System.out.println("the linked list " + num + " includes:");
Employee curEmployee = head;
while (true) {
System.out.printf("=> id = %d name = %s\n", curEmployee.id, curEmployee.name);
if (curEmployee.next == null) {
break;
}
curEmployee = curEmployee.next;
}
}
//search the employee by id
public Employee searchEmployeeById(int id) {
if (head == null) {
System.out.println("the linked list is empty!");
return null;
}
Employee curEmployee = head;
while (true) {
if (curEmployee.id == id) {
break;
}
if (curEmployee.next == null) {
System.out.println("the employee is not found in the current linked list");
break;
}
curEmployee = curEmployee.next;
}
return curEmployee;
}
}