哈希表也叫散列表,是根据关键码值(key-value)而进行直接访问的数据结构。它通过把关键码值映射到表中的某一位置来访问记录,这个映射函数叫做散列函数,存放记录的数组叫散列表。
哈希表可以用数组+链表来实现,也可以用数组+树来实现。本例子用数组+链表来实现。散列函数直接用取余来完成。
public class HashTable {
private int size;
private EmpLinkedList[] empLinkedList;
public static void main(String[] args) {
HashTable hashTable = new HashTable(8);
Scanner scanner = new Scanner(System.in);
String key = "";
while (true) {
System.out.println("欢迎使用散列表");
System.out.println("新增职员:add");
System.out.println("修改职员:update");
System.out.println("查询职员:find");
System.out.println("删除职员:remove");
System.out.println("遍历职员:list");
System.out.println("退出散列表:exit");
key = scanner.next();
switch (key) {
case "add": {
System.out.println("输入id");
int i = scanner.nextInt();
System.out.println("输入姓名");
String name = scanner.next();
Emp emp = new Emp(i, name);
hashTable.add(emp);
break;
}
case "update": {
System.out.println("输入id");
int i = scanner.nextInt();
System.out.println("输入姓名");
String name = scanner.next();
Emp emp = new Emp(i, name);
hashTable.update(emp);
break;
}
case "find": {
System.out.println("输入id");
int i = scanner.nextInt();
System.out.println(hashTable.findEmyById(i).name);
break;
}
case "remove": {
System.out.println("输入id");
int i = scanner.nextInt();
hashTable.removeById(i);
break;
}
case "list": {
hashTable.list();
break;
}
case "exit": {
scanner.close();
System.exit(0);
}
}
}
}
public HashTable(int size) {
this.size = size;
empLinkedList = new EmpLinkedList[size];
for (int i = 0; i < size; i++) {
empLinkedList[i] = new EmpLinkedList();
}
}
public void add(Emp emp) {
int hash = getHash(emp.id);
empLinkedList[hash].add(emp);
}
public Emp findEmyById(int id) {
return empLinkedList[getHash(id)].findById(id);
}
public void removeById(int id) {
empLinkedList[getHash(id)].removeById(id);
System.out.println("删除成功,id为:" + id);
}
public void update(Emp emp) {
empLinkedList[getHash(emp.id)].update(emp);
}
public void list() {
for (int i = 0; i < size; i++) {
empLinkedList[i].list();
}
}
public int getHash(int id) {
return id % size;
}
}
class Emp {
public int id;
public String name;
public Emp next;
public Emp(int id, String name) {
this.id = id;
this.name = name;
}
}
class EmpLinkedList {
private Emp head;
// 头结点就是第一个节点
public void add(Emp emp) {
if (head == null) {
head = emp;
return;
}
Emp temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = emp;
}
public Emp findById(int id) {
Emp temp = head;
if (temp == null) {
System.out.println("链表为空");
return null;
}
while (true) {
if (temp.id == id) {
return temp;
}
if (temp.next == null) {
break;
}
temp = temp.next;
}
return null;
}
public void update(Emp emp) {
Emp temp = head;
if (emp == null || temp == null) {
return;
}
while (true) {
if (temp.id == emp.id) {
temp.name = emp.name;
break;
}
if (temp.next == null) {
break;
}
temp = temp.next;
}
}
public void removeById(int id) {
Emp temp = head;
if (temp == null) {
return;
}
while (temp.next != null) {
if (temp.next.id == id) {
temp.next = temp.next.next;
}
temp = temp.next;
}
}
public void list() {
Emp temp = head;
if (temp == null) {
return;
}
while (true) {
System.out.println("id=" + temp.id + ",name=" + temp.name);
if (temp.next == null) {
break;
}
temp = temp.next;
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)