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;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值