天天看點

1.1哈希表

哈希表思路:

1.需要一個數組,數組中存取的是一個連結清單,這個連結清單可以沒有表頭。

2.需要一個哈希計算的方法,決定傳入的資料存在哪裡

public class HashTable {
    public static void main(String[] args) {
        HashTable hashTable = new HashTable(7);
        User user = new User(1,18,"張三");
        hashTable.addUser(1,user);
        hashTable.findId(1);
    }
    private int maxSize;
    private HashHeadList[] hashTable;

    public HashTable(int max) {
        this.maxSize = max;
        hashTable = new HashHeadList[max];
        for (int i = 0; i < max; i++) {
            hashTable[i] = new HashHeadList();
        }
    }
    public void addUser(int id, User user){
        hashTable[hashValue(id,this.maxSize)].add(user);
    }
    public void showHash(){
        for (int i = 0; i < hashTable.length; i++) {
            System.out.print("第"+i+"條");
            hashTable[i].showHashTable();
        }
    }
    public int hashValue(int id, int maxSize){
        return id % maxSize;
    }
    public void findId(int id){
        int index = hashValue(id,this.maxSize);
        hashTable[index].find(id);
    }
    public void remove(int id){
        int index = hashValue(id,this.maxSize);
        hashTable[index].remove(id);
    }
}
class User{
    public int id;
    private int age;
    private String name;
    public User next;

    public User(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }
    public void prints(){
        System.out.println("id = "+id+", age = "+age+", name = "+name+";");
    }
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}
class HashHeadList{
    public User head;

    public void add(User user){
        if (head == null){
            head = user;
            return;
        }
        User temp = head;
        while (true){
            if (temp.next == null){
                break;
            }
            temp = temp.next;
        }
        temp.next = user;
        System.out.println("添加成功");
    }
    public void showHashTable(){
        if (head == null){
            System.out.println("目前哈希表為空");
            return;
        }
        User temp = head;
        while (true){
            temp.prints();
            if (temp.next == null){
                break;
            }
            temp = temp.next;
        }
    }
    public void find(int id){
        User temp = head;
        while (true){
            if (head == null){
                System.out.println("空");
                return;
            }
            if (temp.id == id){
                System.out.println("找到"+id);
                return;
            }
            if (temp.next == null){
                return;
            }
            temp = temp.next;
        }
    }
    public void remove(int id){
        if (head == null){
            System.out.println("空");
            return;
        }
        User temp =  head;
        User temp2 = null;
        while (true){
            if (temp.next == null){
                System.out.println("沒有找到");
                return;
            }
            if (temp.next.id == id){
                temp.next = temp.next.next;
                break;
            }
            temp = temp.next;
        }
    }
}