数据结构--哈希表

本文详细介绍了如何使用Java实现哈希表,通过数组结合链表的方式模拟哈希表结构。文中给出了Student类、StudentLinked类和HashTable类的实现,包括添加节点、查询所有数据和按ID查询等功能。哈希表的创建、哈希函数以及添加和查询元素的方法都得到了清晰的阐述。
摘要由CSDN通过智能技术生成

一.简介

        哈希表是一种线性结构,可用数组进行模拟

图:

 二.使用数组+链表模拟哈希表

1.创建Student类

/**
 * 使用Student类作为哈希表中链表的节点
 */
public class Student {
    /**
     * 学生id
     */
    public int id;

    /**
     * 学生姓名
     */
    public String name;

    /**
     * 下一个节点
     */
    public Student nextNode;

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

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

2.创建StudentLinked类

public class StudentLinked {

    /**
     * 链表的第一个节点
     */
    public Student head;

    /**
     * 添加一个节点到链表中
     */
    public void addNode(Student student){
        Student temp = head;
        if (head == null){
            head = student;
            return;
        }
        while (true){
            if (temp.nextNode == null){
                temp.nextNode = student;
                return;
            }else{
                if (temp.nextNode.id == student.id){
                    System.out.println(student+"元素重复");
                    return;
                }
            }
            temp = temp.nextNode;
        }
    }

    /**
     * 查询链表中所有的数据
     */
    public void queryAllNode(int i){
        System.out.println(i+"链表=================");
        Student temp = head;
        if (temp == null){
            System.out.print(i+",链表为空");
            return;
        }
        while (true){
            System.out.print(temp+"  ");
            if (temp.nextNode == null){
                return;
            }
            temp = temp.nextNode;
        }
    }

    /**
     * 根据id查询Student对象
     */
    public void queryNodeById(int id){
        Student temp = head;
        if (temp==null){
            System.out.println("空链表");
        }
        while (true){
            if (temp.id == id){
                System.out.println(temp);
                return;
            }
            if (temp.nextNode == null){
                System.out.println("没有该元素");
                return;
            }
            temp = temp.nextNode;
        }
    }
}

3.创建HashTable类

public class HashTable {
    /**
     * 哈希表中的数组
     */
    public StudentLinked[] studentLinkedArray;


    /**
     * 数组的大小
     */
    public int arraySize;

    /**
     * 创建指定大小的数组,数组中的元素是链表
     */
    public  HashTable(int arraySize){
        this.arraySize = arraySize;
        studentLinkedArray = new StudentLinked[arraySize];
        for (int i=0;i<arraySize;i++){
            studentLinkedArray[i] = new StudentLinked();
        }
    }

    /**
     * 哈希函数
     */
    public int currentHashCode(int id){
        return id%arraySize;
    }

    /**
     * 添加节点到哈希表中
     */
    public void addElementHashTable(Student student){
        int i = currentHashCode(student.id);
        studentLinkedArray[i].addNode(student);
    }

    /**
     * 查询哈希表中所有的元素
     */
    public void queryAllEleHashTable(){
        for (int i=0;i<arraySize;i++){
            studentLinkedArray[i].queryAllNode(i);
            System.out.println();
        }
    }

    /**
     * 根据Student的id查询
     */
    public void queryHashTableById(int id){
        int i = currentHashCode(id);
        studentLinkedArray[i].queryNodeById(i);
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

持爱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值