Java数据结构——哈希表

散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。

除留余数法:取关键字被某个不大于哈希表表长m的数p除后所得的余数为散列地址。即 H(key) = key MOD p,p<=m。

本实例使用的是除留余数法。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        HashTable table = new HashTable(10);
        Scanner input = new Scanner(System.in);
        int key = 0;
        int id = 0;
        String name = null;
        while (true) {
            System.out.println("1、添加学生");
            System.out.println("2、遍历哈希表");
            System.out.println("3、查找学生");
            System.out.println("4、退出程序");
            key = input.nextInt();
            switch (key) {
                case 1:
                    System.out.println("请输入学生ID:");
                    id = input.nextInt();
                    System.out.println("请输入学生姓名:");
                    name = input.next();
                    Student st = new Student(id, name);
                    table.add(st);
                    break;
                case 2:
                    table.list();
                    break;
                case 3:
                    System.out.println("请输入学生ID:");
                    id = input.nextInt();
                    table.find(id);
                    break;
                case 4:
                    return;
            }
        }
    }
}

//学生类
class Student {
    public int id;
    public String name;
    Student next;

    public Student(int ID, String na) {
        this.id = ID;
        this.name = na;
    }

}

//链表类
class StuLinkedList {
    //头指针
    private Student head;

    //添加学生信息
    public void add(Student st) {
        //当添加第一个学生时
        if (head == null) {
            head = st;
            return;
        }
        Student temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = st;
    }

    //遍历链表
    public void list(int no) {
        Student temp = head;
        if (head == null) {
            System.out.println("第" + no + "链表为空");
            return;
        }
        System.out.println("第" + no + "链表内容为:");
        while (true) {
            System.out.print(" ## id = " + temp.id + " name = " + temp.name);
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        System.out.println();
    }

    //查找学生信息
    public Student find(int id) {
        if (head == null) {
            System.out.println("链表为空");
            return null;
        }
        Student temp = head;
        while (true) {
            if (temp.id == id) {
                break;
            }
            //当找不到学生信息时
            if (temp.next == null) {
                temp = null;
                break;
            }
            temp = temp.next;
        }
        return temp;
    }
}

//哈希表
class HashTable {
    private int Size;
    private StuLinkedList[] stuArray;

    public HashTable(int size) {
        this.Size = size;
        stuArray = new StuLinkedList[size];
        //对每条链表进行初始化
        for (int i = 0; i < Size; i++) {
            stuArray[i] = new StuLinkedList();
        }
    }

    //取模函数,判断学生放入哪条链表中
    public int hashFun(int id) {
        return id % Size;
    }

    //添加学生
    public void add(Student st) {
        //放入哪条链表
        int no = hashFun(st.id);
        stuArray[no].add(st);
    }

    //遍历哈希表
    public void list() {
        for (int i = 0; i < Size; i++) {
            stuArray[i].list(i);
        }
    }

    //查找学生信息
    public void find(int id) {
        int no = hashFun(id);
        Student st = stuArray[no].find(id);
        if (st != null) {
            System.out.println("第" + no + "条链表中找到,id = " + id);
        } else {
            System.out.println("哈希表中没有该学生信息");
        }
    }

}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值