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("查找失败!");
    		}
    	}
    
    
    
    
    }
    
  • 相关阅读:
    浏览网页的过程
    端口转发和端口映射
    代码审计入门之BlueCMS v1.6 sp1
    php伪协议总结
    phar反序列化
    iOS开发之GCD使用总结
    深入理解Android NDK日志符号化
    Android 开源项目源码解析之DynamicLoadApk 源码解析
    Gilt如何将微服务部署到AWS环境,介绍ION-Roller
    100分程序员的8个习惯
  • 原文地址:https://www.cnblogs.com/airycode/p/5198451.html
Copyright © 2011-2022 走看看