有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

独立链式散列中的java错误

我正在尝试进行单独的链式散列,但我遇到了一个bug,我真的不知道为什么,请记住,我刚刚学习了这个散列和链接列表,所以我的代码可能有点奇怪,或者我不知道该说什么,所以回到主题

公式是 值%10=键

当我输入时,比如说,111 121 131 输出: 首先是111 然后是111 121 但当我输入131时,它变成了111 131 这是为什么? 我会发布完整的代码以防万一

import java.util.*; 
  
// A node of chains 
class HashNode<K, V> 
{ 
    K key; 
    V value; 
  
    // Reference to next node 
    HashNode<K, V> next; 
  
    // Constructor 
    public HashNode(K key, V value) 
    { 
        this.key = key; 
        this.value = value; 
    } 
} 
  
// Class to represent entire hash table 
class Entry<K, V> 
{ 
    // bucketArray is used to store array of chains 
    private ArrayList<HashNode<K, V>> bucket = new ArrayList<>(); 
  
    // Current capacity of array list 
    private int totalBuckets; 
  
    // Current size of array list 
    private int size; 
  
    public Entry() 
    { 
        totalBuckets = 10; 
        size = 0; 
  
        // Create empty chains 
        for (int i = 0; i < totalBuckets; i++) 
            bucket.add(null); 
    } 
  
    public int size() { return size; } 
    public boolean isEmpty() { return size() == 0; } 
  
    // This implements hash function to find index 
    // for a key 
    private int getBucketIndex(K key) 
    { 
        int hashCode = key.hashCode(); 
        int index = hashCode % totalBuckets; 
        return index; 
    } 
  
    // Method to remove a given key 
    public V remove(K key) 
    { 
        // Apply hash function to find index for given key 
        int bucketIndex = getBucketIndex(key); 
  
        // Get head of chain 
        HashNode<K, V> head = bucket.get(bucketIndex); 
  
        // Search for key in its chain 
        HashNode<K, V> prev = null; 
        while (head != null) 
        { 
            // If Key found 
            if (head.key.equals(key)) 
                break; 
  
            // Else keep moving in chain 
            prev = head; 
            head = head.next; 
        } 
  
        // If key was not there 
        if (head == null) 
            return null; 
  
        // Reduce size 
        size--; 
  
        // Remove key 
        if (prev != null) 
            prev.next = head.next; 
        else
            bucket.set(bucketIndex, head.next); 
  
        return head.value; 
    } 
  
    // Returns value for a key 
    public V get(K key) 
    { 
        // Find head of chain for given key 
        int bucketIndex = getBucketIndex(key); 
        HashNode<K, V> head = bucket.get(bucketIndex); 
  
        // Search key in chain 
        while (head != null) 
        { 
            if (head.key.equals(key)) 
                return head.value; 
            head = head.next; 
        } 
  
        // If key not found 
        return null; 
    } 
  
    // Adds a key value pair to hash 
    public void add(K key, V value) 
    { 
        // Find head of chain for given key 
        int bucketIndex = getBucketIndex(key); 
        HashNode<K, V> head = bucket.get(bucketIndex); 
  
        // Check if key is already present 
        while (head != null) 
        { 
            if (head.key.equals(key)) 
            { 
                head.value = value; 
                return; 
            } 
            else 
                head.next=new HashNode<K,V>(key, value);

            head = head.next; 
        } 
  
        // Insert key in chain 
        size++; 
        head = bucket.get(bucketIndex); 
        HashNode<K, V> newNode = new HashNode<K, V>(key, value); 
        newNode.next = head; 
        bucket.set(bucketIndex, newNode); 
    } 
  
    public void printHashTable()
        {
            for (int i = 0; i < totalBuckets; i++)
            {
                System.out.print("\nBucket "+ (i) +" : ");
                // LinkedHashEntry entry = table[i];
                HashNode<K, V> entry = bucket.get(i);
                while (entry != null)
                {
                    System.out.print(entry.value +" ");
                    entry = entry.next;
                }            
            }
        }

    public static void main(String[] args) 
    { 
        Scanner scan = new Scanner(System.in);

        Entry<Integer, Integer>map = new Entry<>(); 
        
        char ch;
        do    
                {
                    System.out.println("Enter key and value");
                    int value = scan.nextInt();
                    map.add(value, value); 
                    /* Display hash table */
                    map.printHashTable();  
                    System.out.println();
                    System.out.println("\nDo you want to continue (Type y or n) \n");
                    ch = scan.next().charAt(0);                        
                } while (ch == 'Y'|| ch == 'y'); 
        scan.close();
    } 
} 

共 (0) 个答案