hashTable、シミュレートされた従業員のストレージ

package org.structure.hashTable;

import java.util.Scanner;

/**
 * hashTable
 * @author cjj_1
 * @date 2020-08-19 11:17
 */
public class HashTableDemo {
    
    
    public static void main(String[] args) {
    
    
        HashTable hashTable = new HashTable(7);
        Scanner s = new Scanner(System.in);
        while (true){
    
    
            System.out.println("add:添加新节点;");
            System.out.println("list:查找列表;");
            System.out.println("find:查找节点;");
            System.out.println("exit:退出;");
            String str = s.next();
            switch (str){
    
    
                case "add":
                    System.out.println("请输入id:");
                    int id = s.nextInt();
                    System.out.println("请输入name:");
                    String name = s.next();
                    hashTable.add(new Emp(id,name));
                    break;
                case "list":
                    hashTable.List();
                    break;
                case "find":
                    System.out.println("请输入id:");
                     id = s.nextInt();
                    hashTable.find(id);
                    break;
                case "exit":
                   s.close();
                    break;
                default:
                    break;
            }
        }
    }
}
class HashTable{
    
    
    EmpList[] empLists ;
    int size ;
   public HashTable(int size){
    
    
        this.size = size;
        empLists = new EmpList[size];
        for(int i =0;i<size;i++){
    
    
            empLists[i] = new EmpList();
        }
    }
    //hashtable 中增加一个 节点
    public void add(Emp emp){
    
    
        int hashIndex = hashFun(emp.id);
        empLists[hashIndex].add(emp);
    }
    //hashtable 中增加一个 节点
    public void List(){
    
    
       EmpList empList;
       for (int i =0;i<size;i++){
    
    
           System.out.printf("第%d个链表中的节点:",i+1);
           empList = empLists[i];
           empList.list(i);
       }
    }
    //找到对应的emp
    public Emp find(int id){
    
    
       int hashIndex = id%size;
       Emp emp = empLists[hashIndex].find(id);
       if(emp!=null)
        System.out.println(emp.toString());
       return emp;
    }
    public int hashFun(int id){
    
    
       return id % this.size;
    }

}
class EmpList{
    
    
    Emp head ;
    Emp cur ;
    //链表增加节点
    public void add(Emp emp){
    
    
        if(head == null){
    
    
            head = emp;
            cur = head.next;
            return;
        }
        cur = emp;
        cur  = cur.next;
    }
    //列表
    public void list(int i){
    
    
        if(head==null){
    
    
            System.out.printf("第%d链表的节点为空\t",i);
            System.out.println();
            return;
        }
        System.out.printf("第%d链表的节点为:\t",i);
        Emp temp = head;
        while (temp!=null){
    
    
            System.out.print(head.toString());
            temp = temp.next;
        }
        System.out.println();
    }
    //查找
    public Emp find(int  id){
    
    
        Emp temp = head;
        while (temp!=null){
    
    
            if(temp.id == id ){
    
    
                return temp;
            }
            temp = temp.next;
        }
        return null;
    }
}
class Emp{
    
    
  protected int id;
  protected String name;
  protected Emp next;
 public Emp(int id,String name){
    
    
     this.id=id;
     this.name =name;
 }
    @Override
    public String toString() {
    
    
        return "Emp{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}


おすすめ

転載: blog.csdn.net/weixin_40128696/article/details/108102718