⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hashdictionary.java

📁 not use Java API to produce two simple java implementation of a dictionary interface, LinkedListDict
💻 JAVA
字号:
package dict;


 public class HashDictionary implements Dictionary{
    
	protected int arraySize=1024;
	Wrapper[] array=new Wrapper[arraySize];
	protected Wrapper p;
   
	public int hashFunction(String key){
		int i=0,sum=0;
		for(;i<key.length();i++)
			sum+=key.charAt(i);
	return sum%arraySize;
	}
	
	public void map(String key,Object value){  
		int i=hashFunction(key);
		if(array[i]==null){
			p=new Wrapper(key,value);
			array[i]=p;
			
		}
		else{
			p=array[i];
			while (p.reference != null)
				p = p.reference;
			if (p.reference == null) {
				p.reference = new Wrapper(key, value);
			}
		}
	}
	
	public Object find(String key){
		int i=hashFunction(key);
		if(array[i]==null)return null;
	    p=array[i];
	    Wrapper tmp=p;
		 for (; tmp != null && !key.equals(tmp.key); tmp = tmp.reference);
		if (tmp == null)
			return null;
		else
			return tmp.value;
	}
	
	public Object delete(String key){
		int i=hashFunction(key);
		if(array[i]==null)return null;
		if(array[i]!=null){
			 if(key.equals(array[i].key)&&(array[i].reference==null)){
			   Wrapper tmp=array[i];
			   array[i]=null;
			   return tmp.value;
			   }
			 if(key.equals(array[i].key)&&(array[i].reference!=null)){
				 Wrapper tmp=array[i];
				 array[i]=array[i].reference;
				 return tmp.value;
			 }
			 else {
				 Wrapper pred=array[i],tmp=array[i].reference;
				 for(;tmp!=null&&!(tmp.key.equals(key));pred=pred.reference,tmp=tmp.reference);
				 if(tmp!=null){
				 pred.reference=tmp.reference;
				 return tmp.value;}
				 else return null;
			 }
		}
		 else return null;
		
	}
	public String toString(){
		int i=0,j;
		String result="[";
		Wrapper beginner=array[0];
		for(;beginner==null;beginner=array[i++]);
		result+=(beginner.key+":"+beginner.value);
		for(j=i;j<arraySize;j++){
		Wrapper pointer=array[j];
		for(;pointer!=null&&beginner!=pointer;pointer=pointer.reference){
			result+=(","+pointer.key+":"+pointer.value);
		   } 
		}
		result+="]";
		return result;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -