哈希表的基本介绍
哈希表流程图
哈希表(散列)——Google上机题
哈希表管理雇员思路分析
代码
表示一个雇员
package com. hashtab;
public class HashTabDemo {
public static void main ( String[ ] args) {
}
}
class Emp {
public int id;
public String name;
public Emp next;
public Emp ( int id, String name) {
this . id = id;
this . name = name;
}
}
创建一个EmpLinkedList,表示链表——创建一个头指针
class EmpLinkedList {
private Emp head;
}
添加雇员到链表
public void add ( Emp emp) {
}
添加雇员到链表-如果是添加第一个雇员
public void add ( Emp emp) {
if ( head == null) {
head = emp;
return ;
}
}
添加雇员到链表-如果不是第一个雇员,则使用辅助的指针,帮助定位到最后
Emp curEmp = head;
while ( true ) {
if ( curEmp. next == null) {
break ;
}
curEmp = curEmp. next;
}
curEmp. next = emp;
遍历链表的雇员信息
public void list ( ) {
}
遍历链表的雇员信息-说明链表为空
public void list ( ) {
if ( head == null) {
System. out. println ( "链表为空" ) ;
return ;
}
遍历链表的雇员信息-当前的链表信息
System. out. println ( "当前的链表信息为" ) ;
Emp curEmp = head;
while ( true ) {
System. out. println ( "=> id=%id name=%s\t, curEmp.id, cueEmp.name" ) ;
if ( curEmp. next == null) {
break ;
}
curEmp = curEmp. next;
}
System. out. println ( ) ;
创建HashTab 管理多条链表
class HashTab {
private EmpLinkedList[ ] empLinkedListsArray;
}
创建HashTab 管理多条链表-构造器
public HashTab ( int size) {
empLinkedListsArray = new EmpLinkedList [ size] ;
}
缩写散列函数,使用一个简单取模法
class HashTab {
private EmpLinkedList[ ] empLinkedListsArray;
private int size;
public HashTab ( int size) {
this . size = size;
empLinkedListsArray = new EmpLinkedList [ size] ;
}
public int hashFun ( int id) {
return id % size;
}
}
创建HashTab 管理多条链表-添加雇员-根据员工的id,得到该员工应当添加到那条链表
public void add ( Emp emp) {
int empLinkedListNo= hashFun ( emp. id) ;
}
创建HashTab 管理多条链表-添加雇员-将emp添加到对应链表中
public void add ( Emp emp) {
int empLinkedListNo= hashFun ( emp. id) ;
empLinkedListsArray[ empLinkedListNo] . add ( emp) ;
创建HashTab 管理多条链表-遍历所有的链表,遍历hashTab
public void list ( ) {
for ( int i = 0 ; i < size; i++ ) {
empLinkedListsArray[ i] . list ( ) ;
}
}
创建哈希表
public class HashTabDemo {
public static void main ( String[ ] args) {
HashTab hashTab = new HashTab ( 7 ) ;
}
}
写一个简单的菜单
String key = "" ;
Scanner scanner = new Scanner ( System. in) ;
while ( true ) {
System. out. println ( "add: 添加雇员" ) ;
System. out. println ( "list: 显示雇员" ) ;
System. out. println ( "exit: 退出雇员" ) ;
}
写一个简单的菜单-添加
key = scanner. next ( ) ;
switch ( key) {
case "add" :
System. out. println ( "输入id" ) ;
int id = scanner. nextInt ( ) ;
System. out. println ( "输入名字" ) ;
String name = scanner. next ( ) ;
Emp emp = new Emp ( id, name) ;
hashTab. add ( emp) ;
break ;
default :
break ;
}
写一个简单的菜单-查看
case "list" :
hashTab. list ( ) ;
break ;
写一个简单的菜单-退出
case "exit" :
scanner. close ( ) ;
System. exit ( 0 ) ;
创建HashTab 管理多条链表-添加雇员-重点:不要分别初始化每个链表
for ( int i = 0 ; i < size; i++ ) {
empLinkedListsArray[ i] = new EmpLinkedList ( ) ;
}
修改bug
public void list ( ) {
for ( int i = 0 ; i < size; i++ ) {
empLinkedListsArray[ i] . list ( ) ;
empLinkedListsArray[ i] . list ( i) ;
}
}
public void list ( int no) {
if ( head == null) {
System. out. println ( "第" + ( no + 1 ) + "链表为空" ) ;
return ;
}
System. out. print ( "第" + ( no + 1 ) + "链表信息为" ) ;
Emp curEmp = head;
while ( true ) {
System. out. printf ( "=> id=%d name=%s\t" , curEmp. id, curEmp. name) ;
if ( curEmp. next == null) {
break ;
创建HashTab 管理多条链表-根据id查找雇员
public Emp findEmpById ( int id) {
}
创建HashTab 管理多条链表-根据id查找雇员-判断链表是否为空-链表为空
if ( head == null) {
System. out. println ( "链表为空" ) ;
return null;
}
创建HashTab 管理多条链表-根据id查找雇员-判断链表是否为空-辅助指针
Emp curEmp = head;
while ( true ) {
if ( curEmp. id == id) {
break ;
}
if ( curEmp. next == null) {
curEmp = null;
}
curEmp = curEmp. next;
}
return curEmp;
创建HashTab 管理多条链表-根据输入的id查找雇员-使用散列函数确定那条链表查找
public void findEmpById ( int id) {
int empLinkedListNo = hashFun ( id) ;
Emp emp = empLinkedListsArray[ empLinkedListNo] . findEmpById ( id) ;
}
创建HashTab 管理多条链表-根据输入的id查找雇员-判断下有没有找到
if ( emp != null) {
System. out. printf ( "在第%d条链表中找到雇员id = %d\n" , ( empLinkedListNo + 1 ) , id) ;
} else {
System. out. println ( "在哈希链表中,没有找到该雇员" ) ;
}
写一个简单的菜单-查找雇员
while ( true ) {
System. out. println ( "add: 添加雇员" ) ;
System. out. println ( "list: 显示雇员" ) ;
System. out. println ( "find: 查找雇员" ) ;
System. out. println ( "exit: 退出雇员" ) ;
key = scanner. next ( ) ;
case "find" :
System. out. println ( "输入要查找的id" ) ;
id = scanner. nextInt ( ) ;
hashTab. findEmpById ( id) ;
break ;
if ( curEmp. next == null) {
curEmp = null;
break ;
完整代码
package com. hashtab;
import java. util. Scanner;
public class HashTabDemo {
public static void main ( String[ ] args) {
HashTab hashTab = new HashTab ( 7 ) ;
String key = "" ;
Scanner scanner = new Scanner ( System. in) ;
while ( true ) {
System. out. println ( "add: 添加雇员" ) ;
System. out. println ( "list: 显示雇员" ) ;
System. out. println ( "find: 查找雇员" ) ;
System. out. println ( "exit: 退出雇员" ) ;
key = scanner. next ( ) ;
switch ( key) {
case "add" :
System. out. println ( "输入id" ) ;
int id = scanner. nextInt ( ) ;
System. out. println ( "输入名字" ) ;
String name = scanner. next ( ) ;
Emp emp = new Emp ( id, name) ;
hashTab. add ( emp) ;
break ;
case "list" :
hashTab. list ( ) ;
break ;
case "find" :
System. out. println ( "输入要查找的id" ) ;
id = scanner. nextInt ( ) ;
hashTab. findEmpById ( id) ;
break ;
case "exit" :
scanner. close ( ) ;
System. exit ( 0 ) ;
default :
break ;
}
}
}
}
class HashTab {
private EmpLinkedList[ ] empLinkedListsArray;
private int size;
public HashTab ( int size) {
this . size = size;
empLinkedListsArray = new EmpLinkedList [ size] ;
for ( int i = 0 ; i < size; i++ ) {
empLinkedListsArray[ i] = new EmpLinkedList ( ) ;
}
}
public void add ( Emp emp) {
int empLinkedListNo = hashFun ( emp. id) ;
empLinkedListsArray[ empLinkedListNo] . add ( emp) ;
}
public void list ( ) {
for ( int i = 0 ; i < size; i++ ) {
empLinkedListsArray[ i] . list ( i) ;
}
}
public void findEmpById ( int id) {
int empLinkedListNo = hashFun ( id) ;
Emp emp = empLinkedListsArray[ empLinkedListNo] . findEmpById ( id) ;
if ( emp != null) {
System. out. printf ( "在第%d条链表中找到雇员id = %d\n" , ( empLinkedListNo + 1 ) , id) ;
} else {
System. out. println ( "在哈希链表中,没有找到该雇员" ) ;
}
}
public int hashFun ( 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 curEmp = head;
while ( true ) {
if ( curEmp. next == null) {
break ;
}
curEmp = curEmp. next;
}
curEmp. next = emp;
}
public void list ( int no) {
if ( head == null) {
System. out. println ( "第" + ( no + 1 ) + "链表为空" ) ;
return ;
}
System. out. print ( "第" + ( no + 1 ) + "链表信息为" ) ;
Emp curEmp = head;
while ( true ) {
System. out. printf ( "=> id=%d name=%s\t" , curEmp. id, curEmp. name) ;
if ( curEmp. next == null) {
break ;
}
curEmp = curEmp. next;
}
System. out. println ( ) ;
}
public Emp findEmpById ( int id) {
if ( head == null) {
System. out. println ( "链表为空" ) ;
return null;
}
Emp curEmp = head;
while ( true ) {
if ( curEmp. id == id) {
break ;
}
if ( curEmp. next == null) {
curEmp = null;
break ;
}
curEmp = curEmp. next;
}
return curEmp;
}
}