线性探测哈希表的java代码

深圳装修网

package chapter11;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


class DataItem{
private int iData;
public DataItem(int i){
iData=i;
}
public int getKey(){
return iData;
}
}
class HashTable{
private DataItem[] hashArray;
private int arraySize;
private DataItem nonItem;
public HashTable(int size){
arraySize=size;
hashArray=new DataItem[arraySize];
nonItem=new DataItem(-1);
}
public void displayTable(){
System.out.print("Table:");
for(int j=0;j<arraySize;j++){
if(hashArray[j]!=null)
System.out.print(hashArray[j].getKey()+" ");
else
System.out.print("** ");
}
System.out.println("");
}
public int hashFunc(int key){
return key%arraySize;
}
public void insert(DataItem item){
int key=item.getKey();
int hashVal=hashFunc(key);
while(hashArray[hashVal]!=null&&hashArray[hashVal].getKey()!=-1){
++hashVal;
hashVal%=arraySize;
}
hashArray[hashVal]=item;
}
public DataItem delete(int key){
int hashVal=hashFunc(key);
while(hashArray[hashVal]!=null){
if(hashArray[hashVal].getKey()==key){
DataItem item=hashArray[hashVal];
hashArray[hashVal]=nonItem;
return item;
}else{
++hashVal;
hashVal%=arraySize;
}
}
return null;
}
public DataItem find(int key){
int hashVal=hashFunc(key);
while(hashArray[hashVal]!=null){
if(hashArray[hashVal]!=nonItem&&hashArray[hashVal].getKey()==key){
return hashArray[hashVal];
}else{
++hashVal;
hashVal%=arraySize;
}
}
return null;
}
}
public class HashTableApp {
public static void main(String args[])throws IOException{
DataItem aDataItem;
int aKey,size,n,keysPerCell;
System.out.print("Enter size of hash table: ");
size=getInt();
System.out.print("Enter initial number of items: ");
n=getInt();
keysPerCell=10;
HashTable theHashTable=new HashTable(size);
for(int j=0;j<n;j++){
aKey=(int)(java.lang.Math.random()*keysPerCell*size);
aDataItem=new DataItem(aKey);
theHashTable.insert(aDataItem);
}
while(true){
System.out.print("Enter first letter of ");
System.out.print("show,insert,delete,or fild: ");
char choice=getChar();
switch(choice){
case 's':
theHashTable.displayTable();
break;
case 'i':
System.out.print("Enter key value to insert: ");
aKey=getInt();
aDataItem=new DataItem(aKey);
theHashTable.insert(aDataItem);
break;
case 'd':
System.out.print("Enter key value to delete: ");
aKey=getInt();
theHashTable.delete(aKey);
break;
case 'f':
System.out.print("Enter key value to find: ");
aKey=getInt();
aDataItem=theHashTable.find(aKey);
if(aDataItem!=null){
System.out.println("Found "+aKey);
}else
System.out.println("Could not find "+aKey);
break;
default:
System.out.print("Invalid entery\n");
}
}
}
public static String getString()throws IOException{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String s=br.readLine();
return s;
}
public static char getChar() throws IOException{
String s=getString();
return s.charAt(0);
}
public static int getInt()throws IOException{
String s=getString();
return Integer.parseInt(s);
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值