zoukankan      html  css  js  c++  java
  • 散列表Java实现

    package 散列表;
    
    import java.util.Scanner;
    
    public class HashSearch {
    
    	public static int data[] = {69,65,90,37,92,6,28,54};
    	public static int hash[] = new int[13];
    	
    	
    	//将关键字插入到散列表中
    	public static void insertHash(int hash[],int m,int data){
    		int i = 0;
    		i = data%13;//计算散列位置
    		while (hash[i] >0) {//位置已经被占用
    			i = (++i)%m;//先行探索解决冲突
    		}
    		hash[i] = data;
    	}
    
    	//创建散列表
    	public static void createHash(int hash[],int m,int data[],int n){
    		for (int i = 0;i<hash.length;i++) {
    			hash[i] = 0;
    		}
    		for (int i = 0;i<n;i++) {
    			insertHash(hash, m, data[i]);
    		}
    		
    	}
    
    	//散列表的查找函数的编写
    	public static int hashSearch(int [] hash,int m,int key){
    		int i = 0;
    		i = key%13;
    		while (hash[i] > 0 && hash[i] != key) {//判断是不是冲突
    			i = (++i)%m;
    		}
    		if (hash[i] == 0) {
    			return -1;
    		}else{
    			return i;
    		}
    	
    	}
    
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		//调用函数创建散列表
    		createHash(hash, 13, data, 8);
    		System.out.println("散列表各元素的值:");
    		for(int i = 0;i<13;i++){
    			System.out.print(hash[i]+" ");
    		}
    		System.out.println();
    		System.out.println("输入查找的关键字");
    		int key = input.nextInt();
    		int pos = hashSearch(hash, 13, key);
    		if (pos > 0) {
    			System.out.printf("查找成功,该关键字位于数组的第%d个位置
    ",pos);
    		}else{
    			System.out.println("查找失败!");
    		}
    	}
    
    
    
    
    }
    
  • 相关阅读:
    TestCase NotePad3.0 of robotium
    一些小的东东
    面试ASP.NET程序员的笔试题和机试题
    verify the text views its easy to use getView from the adapter
    巧用Cacls.exe命令来修改文件访问控制权限
    Monkey test
    monkeyrunner test
    Autotest NotePad with robotium
    网站新技术知识
    序列化
  • 原文地址:https://www.cnblogs.com/airycode/p/5198451.html
Copyright © 2011-2022 走看看