java 实现hash表

java 实现hash表,第1张

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5697373.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存