HashTable簡單實現思路

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

public class HashTable {
	private DataItem hashArray[];
	private int arraySize;
	private DataItem noneItem;
	public HashTable(int size){
		arraySize = size;
		hashArray = new DataItem[arraySize];
		noneItem = new DataItem(-1);
	}
	
	public void displayTable(){
		System.out.print("Table:");
		for(int i=0;i<arraySize;i++){
			if(hashArray[i]!=null){
				System.out.print(hashArray[i].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 = hashVal%arraySize;
		}
		hashArray[hashVal] = item;
	}
	
	public DataItem delete(int key){
		int hashVal  = hashFunc(key);
		while(hashArray[hashVal]!=null){
			if(hashArray[hashVal].getKey()==key){
				DataItem tmp = hashArray[hashVal];
				hashArray[hashVal] = noneItem;
				return tmp;
			}
			hashVal++;
			hashVal = hashVal%arraySize;
		}
		return null;
	}
	
	public DataItem find(int key){
		int hashVal = hashFunc(key);
		while(hashArray[hashVal]!=null){
			if(hashArray[hashVal].getKey()==key){
				return hashArray[hashVal];
			}
			hashVal++;
			hashVal = hashVal%arraySize;
		}
		return null;
	}
}</span>
<span style="font-size:24px;">
</span>
<span style="font-size:24px;">測試:</span>
<span style="font-size:24px;"></span><pre name="code" class="java">package com.liebao32;
import java.io.*;
public class HashTableApp {
	public static void main(String[] args) throws Exception {
		DataItem item ;
		int key,size,n,keysPerCell;
		System.out.print("Enter size of hash table: ");
		size = getInt();
		System.out.print("Enter initial number of item: ");
		n = getInt();
		keysPerCell = 10;
		HashTable table = new HashTable(size);
		for(int i=0;i<n;i++){
			key = (int)(Math.random()*keysPerCell*size);
			item = new DataItem(key);
			table.insert(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(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());
	}
	
}




發佈了47 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章