码迷,mamicode.com
首页 > 编程语言 > 详细

哈希表解决冲突链地址java

时间:2015-02-10 22:58:56      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

package ch17;

import java.math.BigInteger;

public class HashTable {
private LinkList[] arr;
	
	/**
	 * 默认的构造方法
	 */
	public HashTable() {
		arr = new LinkList[100];
	}
	
	/**
	 * 指定数组初始化大小
	 */
	public HashTable(int maxSize) {
		arr = new LinkList[maxSize];
	}
	
	/**
	 * 插入数据
	 */
	public void insert(Info info) {
		//获得关键字
		String key = info.getKey();
		//关键字所自定的哈希数
		int hashVal = hashCode(key);
		if(arr[hashVal] == null) {
			arr[hashVal] = new LinkList();
		}
		arr[hashVal].insertFirst(info);
	}
	
	/**
	 * 查找数据
	 */
	public Info find(String key) {
		int hashVal = hashCode(key);
		return arr[hashVal].find(key).info;
	}
	
	/**
	 * 删除数据
	 * @param key
	 * @return
	 */
	public Info delete(String key) {
		int hashVal = hashCode(key);
		return arr[hashVal].delete(key).info;
	}
	
	public int hashCode(String key) {
//		int hashVal = 0;
//		for(int i = key.length() - 1; i >= 0; i--) {
//			int letter = key.charAt(i) - 96;
//			hashVal += letter;
//		}
//		return hashVal;
		
		BigInteger hashVal = new BigInteger("0");
		BigInteger pow27 = new BigInteger("1");
		for(int i = key.length() - 1; i >= 0; i--) {
			int letter = key.charAt(i) - 96;
			BigInteger letterB = new BigInteger(String.valueOf(letter));
			hashVal = hashVal.add(letterB.multiply(pow27));
			pow27 = pow27.multiply(new BigInteger(String.valueOf(27)));
		}
		return hashVal.mod(new BigInteger(String.valueOf(arr.length))).intValue();
	}

	
}
=====================================
package ch17;

public class Info {

	private String   key;
	private String name;
	
	public Info(String key,String name){
		this.key = key;
		this.name = name;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	
	
	
}
==========================================
package ch17;

public class LinkList {

	//头结点
	private Node first;
	
	public LinkList(){
		first = null;
	}
	//插入一个节点在头结点
	public void insertFirst(Info info){
		Node node = new Node(info);
		node.next = first;
		first = node;
		
	}
	
	/**
	 * 删除一个节点在头结点的后面删除
	 * 
	 */
	public Node deleteFirst(){
		Node tmp = first;
		first = tmp.next;
		return tmp;
		
		
		
	}
	
	/**
	 * 查找方法的实现
	 */
	public Node find(String key){
		Node current = first;
		while(!key.equals(current.info.getKey())){
			if(current.next == null){
				return null;
			}
			current = current.next;
		}
		return current;
	}
	
	/**
	 * 删除方法根据数据域删除数据
	 */
	public Node delete(String key){
		Node current = first;
		Node previous = first;
		while(!key.equals(current.info.getKey())) {
			if(current.next == null) {
				return null;
			}
			previous = current;
			current = current.next;
		}
		
		if(current == first) {
			first = first.next;
		} else {
			previous.next = current.next;
		}
		return current;
		

		
		
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
}
====================================
package ch17;
/**
 * 链结点相当于车厢
 * @author Administrator
 *
 */
public class Node {

	public Info info;
	public  Node next;
	public Node(Info info){
		this.info = info;
	}
	
}
====================================
package ch17;

public class TestHashTable {

	
	public static void main(String[] args) {
		HashTable ht = new HashTable();
		ht.insert(new Info("a","张三"));
		ht.insert(new Info("ct","李四"));
		ht.insert(new Info("b","王五"));
		ht.insert(new Info("dt","赵六"));
		
		System.out.println(ht.find("a").getName());
		System.out.println(ht.find("ct").getName());
		System.out.println(ht.find("b").getName());
		System.out.println(ht.find("dt").getName());
		
//		System.out.println(ht.hashCode("a"));
//		System.out.println(ht.hashCode("ct"));
		
		System.out.println(ht.delete("a").getName());
		System.out.println(ht.find("a").getName());


	}

}

  

哈希表解决冲突链地址java

标签:

原文地址:http://www.cnblogs.com/aicpcode/p/4285080.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!