栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 实现hash表

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 实现hash表

// 节点信息
public class Node {
    public int id;
    public String name;
    public Node next;

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

// 节点链表
public class HashTableList {
    // 链表头指针
    private Node head;

    // 链表末尾添加
    public void add(Node node) {
        if (head == null) {
            head = node;
            return;
        }
        Node tmp = head;
        while (tmp.next != null) {
            tmp = tmp.next;
        }
        tmp.next = node;
    }

    // 根据id查找node
    public Node findNode(int id) {
        if (head == null) {
            return null;
        }
        Node tmp = head;
        while (tmp != null && tmp.id != id) {
            tmp = tmp.next;
        }
        return tmp;
    }

    // 遍历
    public void show(int no) {
        if (head == null) {
            System.out.println("第" + (no + 1) + "条链表为空");
            return;
        }
        System.out.print("第" + (no + 1) + "条链表信息为:");
        Node tmp = head;
        while (tmp.next != null) {
            System.out.printf("==>id = %d name = %st", tmp.id, tmp.name);
            tmp = tmp.next;
        }
        System.out.printf("==>id = %d name = %sn", tmp.id, tmp.name);
    }
}

// hash表
public class HashTable {
    private HashTableList[] nodeListArray;
    private int size;
    public HashTable(int size) {
        this.size = size;
        nodeListArray = new HashTableList[size];

        // 分别初始化每一条链表
        for (int i = 0; i < size; i++) {
            nodeListArray[i] = new HashTableList();
        }
    }

    //散列函数(取模法)
    public int hashFun(int id) {
        return id % size;
    }


    // 添加
    public void add(Node node) {
        // 根据node的id判断添加到哪一条链表
        int hashTableListNo = hashFun(node.id);
        nodeListArray[hashTableListNo].add(node);
    }

    // 查找
    public void findNode(int id) {
        Node node = nodeListArray[hashFun(id)].findNode(id);
        if (node == null) {
            System.out.println("哈希表中没有此节点信息");
        } else {
            System.out.printf("在第%d条链表中找到结点%dn", hashFun(id) + 1, id);
        }
    }

    // 遍历
    public void show() {
        for (int i = 0; i < size; i++) {
            nodeListArray[i].show(i);
        }
    }
}


// 主函数
public static void main(String[] args) {
    HashTable hashTable = new HashTable(10);
    String key = "";
    Scanner scanner = new Scanner(System.in);
    while (true) {
        System.out.println("add: 添加结点");
        System.out.println("show: 遍历结点");
        System.out.println("find: 查找结点");
        System.out.println("exit: 退出");
        key = scanner.next();
        switch (key) {
            case "add":
                System.out.println("输入id:");
                int id = scanner.nextInt();
                System.out.println("输入name");
                String name = scanner.next();
                Node node = new Node(id, name);
                hashTable.add(node);
                break;
            case "find":
                System.out.println("输入id:");
                id = scanner.nextInt();
                hashTable.findNode(id);
                break;
            case "show":
                hashTable.show();
                break;
            case "exit":
                scanner.close();
                System.exit(0);
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/697238.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号