public class hashTest {
public static void main(String args[]) {
HashTab hashTab = new HashTab(7);
Scanner scan=new Scanner(System.in);
while (true){
System.out.println("1:添加");
System.out.println("2:删除");
System.out.println("3:遍历");
System.out.println("4:寻找");
System.out.println("5:退出");
System.out.print("请输入选择:");
boolean isFlag=false;
String str=scan.next();
switch (str){
case "1":
System.out.print("姓名:");
String name=scan.next();
System.out.print("id:");
int id=scan.nextInt();
Emp emp=new Emp(id,name);
hashTab.add(emp);
break;
case "2":
System.out.println("id:");
int id2=scan.nextInt();
hashTab.del(id2);
break;
case "3":
hashTab.list();
break;
case "4":
System.out.println("id:");
int id1=scan.nextInt();
hashTab.find(id1);
break;
case "5":
System.out.println("确定(Y) 取消(N):");
String str2=scan.next();
if (str2.equals("Y")||str2.equals("y")){
isFlag=true;
}
break;
}
if (isFlag){
break;
}
}
}
}
class Emp {
private int id;
private String name;
public Emp next;
public Emp(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Emp{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
class EmpLinkedList{
private Emp head;
public EmpLinkedList() {
}
public Emp getHead() {
return head;
}
public void setHead(Emp head) {
this.head = head;
}
public void add(Emp emp){
if (head==null){
head=emp;
return;
}
Emp temp=head;
while (true){
if (temp.next==null){
break;
}
temp=temp.next;
}
temp.next=emp;
}
public void del(int id){
if (head==null){
System.out.println("链表为空,无法删除");
return;
}
Emp temp=head;
if (temp.getId()==id){
head=head.next;
System.out.println("删除成功");
return;
}
boolean isflag=false;
while (true){
if (temp.next.getId()==id){
isflag=true;
break;
}
if(temp.next==null){
break;
}
temp=temp.next;
}
if (isflag){
temp.next=temp.next.next;
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
}
public void find(int id){
if (head==null){
System.out.println("链表为空");
return;
}
Emp temp=head;
boolean isflag=false;
while (true){
if (temp.getId()==id){
isflag=true;
break;
}
if(temp.next==null){
break;
}
temp=temp.next;
}
if (isflag){
System.out.println(temp);
}else {
System.out.println("没有这个员工");
}
}
public void list(){
if (head==null){
System.out.println("链表为空,无法遍历");
return;
}
Emp temp=head;
while (true){
System.out.print(temp+"\t");
if (temp.next==null){
break;
}
temp=temp.next;
}
}
}
class HashTab{
int maxSize;
EmpLinkedList[] listArry;
public HashTab(int maxSize) {
this.maxSize=maxSize;
listArry=new EmpLinkedList[maxSize];
for (int i=0;i<maxSize;i++){
listArry[i]=new EmpLinkedList();
}
}
public int hash(int id){
int hashCode=id%10;
return hashCode;
}
public void add(Emp emp) {
int hashCode = hash(emp.getId());
EmpLinkedList list = listArry[hashCode];
list.add(emp);
}
public void del(int id){
int hashCode=hash(id);
EmpLinkedList list=listArry[hashCode];
list.del(id);
}
public void list(){
for (int i=0;i<maxSize;i++){
listArry[i].list();
System.out.println();
}
}
public void find(int id){
int hashCode=hash(id);
EmpLinkedList list=listArry[hashCode];
list.find(id);
}
}
共勉!