问题描述
我正在尝试进行单独的链接散列,但我遇到了一个错误,我真的不知道为什么说吧,回到正题
公式是 值 % 10 = 键
当我输入时说,111 121 131 输出 : 首先是 111 然后 111 121 但是当我输入 131 时它变成了 111 131 它删除了 121 为什么会这样? 我会发布完整的代码以防万一
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,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();
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)