HaahTable 再Hash

<span style="font-size:24px;">package com.liebao33;

public class DataItem {
	private int iData;
	public DataItem(int ii){
		iData = ii;
	}
	public int getKey(){
		return iData;
	}
}
</span>
<pre name="code" class="java"><span style="font-size:24px;">package com.liebao33;

public 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(null!=hashArray[j]){
				System.out.print(hashArray[j].getKey()+" ");
			}else{
				System.out.print(" ** ");
			}
		}
		System.out.println();
	}
	
	public int hashFunc1(int key){
		return key%arraySize;
	}
	
	public int hashFunc2(int key){
		//stepsize=constant-(key%constant)
		//constant is 质数,less than arraySize
		return 5-(key%5);//>0
	}
	public void insert(int key,DataItem item){
		int hashVal = hashFunc1(key);//first hash
		int stepSize = hashFunc2(key);//again hash
		while(hashArray[hashVal]!=null && hashArray[hashVal].getKey()!=-1){
			hashVal+=stepSize;
			hashVal %= arraySize;
		}
		hashArray[hashVal] = item;
	}
	
	public DataItem delete(int key){
		int hashVal = hashFunc1(key);//first hash
		int stepSize = hashFunc2(key);//again hash
		while(hashArray[hashVal]!=null){
			if(hashArray[hashVal].getKey()==key){
				DataItem tmp = hashArray[hashVal];
				hashArray[hashVal] = nonItem;
				return tmp;
			}
			hashVal+=stepSize;
			hashVal%=arraySize;
		}
		return null;
	}
	
	public DataItem find(int key){
		int hashVal = hashFunc1(key);//first hash
		int stepSize = hashFunc2(key);//again hash
		while(hashArray[hashVal]!=null){
			if(hashArray[hashVal].getKey()==key){
				return hashArray[hashVal];
			}
			hashVal+=stepSize;
			hashVal%=arraySize;
		}
		return null;
	}
}	
</span>


 

测试:

<span style="font-size:24px;">package com.liebao33;
import java.io.*;

public class DoubleApp {
	public static void main(String[] args) throws Exception{
		DataItem item ;
		int key,size,n;
		System.out.print("Enter size of hash table: ");
		size = getInt();
		System.out.print("Enter initial number of item: ");
		n = getInt();
		HashTable table = new HashTable(size);
		for(int i=0;i<n;i++){
			key = (int)(Math.random()*2*size);
			item = new DataItem(key);
			table.insert(key,item);
		}
		while(true){
			System.out.println("Enter first letter of show,insert,delete,find:");
			char choice = getChar();
			switch (choice) {
			case 's':
				table.displayTable();
				break;
			case 'i':
				System.out.print("Enter key value to insert :");
				key = getInt();
				item = new DataItem(key);
				table.insert(key,item);
				break;
			case 'd':
				System.out.print("Enter key value to delete :");
				key = getInt();
				table.delete(key);
				break;
			case 'f':
				System.out.print("Enter key value to find :");
				key = getInt();
				item = table.find(key);
				if(item!=null){
					System.out.println("Find: "+key);
				}else{
					System.out.println("Could not find: "+key);
				}
				break;
			default:
				System.out.print("Invalid entry\n");
			}
		}
	}
	
	public static String getString() throws Exception{
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		return br.readLine();
	}
	public static char getChar() throws Exception{
		return getString().charAt(0);
	}
	public static int getInt() throws Exception{
		return Integer.parseInt(getString());
	}
}</span>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值