哈希表 java

    技术2026-08-06  19

    package com.数据结构; import java.util.Hashtable; import java.util.Scanner; public class 哈希表 { //分别采用取模法 和idea实现哈希表 public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str; Hashtabl hash = new Hashtabl(5); while (true) { System.out.println("输入add 增加员工"); System.out.println("输入dalete 删除员工"); System.out.println("输入modify 修改员工信息"); System.out.println("输入seek 查找员工信息"); System.out.println("输入list 获取所有员工信息"); System.out.println("输入exit 退出程序"); str = sc.next(); if (str.equals("add")) { System.out.println("请输入员工id:"); int str1 = sc.nextInt(); System.out.println("请输入员工姓名:"); String str2 = sc.next(); hash.add(new Emp(str1,str2)); } else if (str.equals("delete")) { System.out.println("请输入员工id:"); int str1 = sc.nextInt(); hash.delete(str1); } else if (str.equals("modify")) { System.out.println("请输入员工id:"); int str1 = sc.nextInt(); System.out.println("请输入员工姓名:"); String str2 = sc.next(); hash.modify(new Emp(str1,str2)); } else if (str.equals("seek")) { System.out.println("请输入员工id:"); int str1 = sc.nextInt(); hash.seek(str1); } else if (str.equals("list")) { hash.list(); } else if (str.equals("exit")) { return; } else { System.out.println("输入错误 请重新输入"); } } } } class Hashtabl{//哈希表 public int size ; public LinkedListTest[] linked ; public Hashtabl(int size) { this.size=size ; this.linked=new LinkedListTest[size];//这里需要注意 我们只new 除了一个数组 但是链表我们并没有new for(int i=0;i<size;i++){ this.linked[i]=new LinkedListTest(); } } public void add(Emp emp){ //增加员工 int index=hashFun(emp.id); this.linked[index].add(emp); } public void delete(int id){//删除指定id员工 int index =hashFun(id); linked[index].delete(id); } public void modify(Emp emp){//修改相同id员工姓名 int index=hashFun(emp.id); linked[index].modify(emp); } public void seek(int id){//查找指定id员工姓名 int index=hashFun(id); System.out.println(linked[index].seek(id)); } public void list(){//遍历数组 for(int i=0;i<size;i++){ System.out.print("第"+(i+1)+"条链表"); linked[i].list(); System.out.println(); } } public int hashFun(int id){//手写简易的哈希函数 获得员工应该插入的位置 return id%size; } } class LinkedListTest{ //链表类 public Emp head ; //头节点直接指向的第一个元素 public LinkedListTest() { } public void add(Emp emp){ //增加员工 if(head==null){ head=emp; }else{ Emp temp = new Emp(); temp=head; while(temp.next!=null){ temp=temp.next; } temp.next=emp; } } public void delete(int id){//删除指定id员工 if(head.id==id){ head=head.next; return ; } Emp temp =new Emp(); boolean flag = false; temp.next=head; while(true){ if(temp.next==null){ break; }else if(temp.next.id==id){ flag=true; break; } temp=temp.next; } if(flag){ temp.next=temp.next.next; }else{ System.out.println("删除的元素不存在~~~~~~~"); } } public void modify(Emp emp){//修改相同id员工的姓名 Emp temp =head; boolean flag = false; while(true){ if(emp==null){ break; }else if(temp.id==temp.id){ flag = true; break; } temp=temp.next; } if(flag){ temp.name=emp.name; }else{ System.out.println("要修改的id不存在~~~~~~"); } } public String seek(int id){//查找指定id员工姓名 Emp temp = head; boolean flag = false; while(true){ if(temp==null){ break; }else if(temp.id==id){ return temp.name; } temp=temp.next; } return "不存在该员工"; } public void list(){//遍历整个链表 Emp temp=head; while(temp!=null){ System.out.print(" -->"+temp+" "); temp=temp.next; } } } class Emp{ //员工类 public int id ; public String name ; public Emp next ; public Emp() { } public Emp(int id, String name) { this.id = id; this.name = name; } @Override public String toString() { return "员工id=" + id + " 员工name='" + name + '\''; } }
    Processed: 0.012, SQL: 9