//时间复杂度为O(n^2)
import java.util.Arrays;
import java.util.Objects;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
HashTable hashTable=new HashTable();
hashTable.show();
hashTable.system();
}
}
class student{
String name;
int key;
int hashcode;
student next;
student(){
this.key=-1;
this.name=null;
this.hashcode=0;
this.next=null;
}
student(String name){
this.name=name;
this.hashcode=Math.abs(this.name.hashCode());
this.key=this.hashcode%11;
if(this.key<0){
this.key+=11;
}
this.next=null;
}
}
class HashTable{
student []classmates;
student []hashtable;
HashTable() {
this.classmates = new student[40];
this.hashtable = new student[12];
for(int i=0;i<11;i++)this.hashtable[i]=new student();
this.classmates[0] = new student("zhangsan");
this.classmates[1] = new student("zhangsi");
this.classmates[2] = new student("zhangwu");
this.classmates[3] = new student("zhangliu");
this.classmates[4] = new student("zhangqi");
this.classmates[5] = new student("zhangba");
this.classmates[6] = new student("zhangjiu");
this.classmates[7] = new student("zhangshi");
this.classmates[8] = new student("wangda");
this.classmates[9] = new student("wanger");
this.classmates[10] = new student("wangsan");
this.classmates[11] = new student("wangssi");
this.classmates[12] = new student("wangwu");
this.classmates[13] = new student("wangliu");
this.classmates[14] = new student("wangqi");
this.classmates[15] = new student("wangba");
this.classmates[16] = new student("wangjiu");
this.classmates[17] = new student("wanglaoda");
this.classmates[18] = new student("wang6");
this.classmates[19] = new student("king");
this.classmates[20] = new student("lucy");
this.classmates[21] = new student("jane");
this.classmates[22] = new student("zouyi");
this.classmates[23] = new student("zouer");
this.classmates[24] = new student("zousan");
this.classmates[25] = new student("zousi");
this.classmates[26] = new student("zouywu");
this.classmates[27] = new student("zouyliu");
this.classmates[28] = new student("zouysun");
this.classmates[29] = new student("zoumon");
this.classmates[30] = new student("zoutues");
this.classmates[31] = new student("zousat");
this.classmates[32] = new student("zhangwen");
this.classmates[33] = new student("zhan");
this.classmates[34] = new student("angsan");
this.classmates[35] = new student("ngsan");
this.classmates[36] = new student("guess");
this.classmates[37] = new student("zn");
this.classmates[38] = new student("batman");
this.classmates[39] = new student("spiderman");
for(int i=0;i<11;i++)
hashtable[i].key=-1;
int sum=0;
for(int i=0;i<40;i++){
int index=0;
student temp=hashtable[classmates[i].key];
sum+=1;
if(temp.key==-1){
hashtable[classmates[i].key]=classmates[i];
}
else{
while(temp.next!=null){
temp=temp.next;
sum+=1;
}
temp.next=classmates[i];
}
}
System.out.println("平均查找长度为"+sum/40);
}
void show(){
for(int i=0;i<11;i++){
System.out.print(i+":");
student temp=hashtable[i];
while(temp!=null&&temp.key!=-1){
System.out.print(temp.name+" "+ temp.hashcode+"->");
temp=temp.next;
}
System.out.print("null");
System.out.println();
}
}
void search(String name) {
student classmate = new student(name);
student temp = hashtable[classmate.key];
while (temp != null && temp.key != -1){
if(Objects.equals(temp.name, classmate.name)){
System.out.println("查询到..");
System.out.println(temp.name+":"+temp.hashcode);
return;
}
temp = temp.next;
}
System.out.println("查无此人");
}
void system(){
Scanner scanner=new Scanner(System.in);
System.out.println("输入q退出");
while(true){
System.out.print("输入名字:");
String name=scanner.nextLine();
if(Objects.equals(name, "q")){
System.out.println("欢迎下次使用");
return;
}
else
search(name);
}
}
}
散列表 班级学生查询
最新推荐文章于 2024-10-11 17:30:36 发布