心若明鏡,哈希從容

實現哈希表的構造和查找算法,要求:用除留餘數法構造哈希函數,分別用一次探測再散列、二次探測再散列解決衝突。

想法:有構造哈希表,查找元素,插入一個元素,如果查找到一個哈希表裏面沒有的元素應該插入到哈希表中。

小小希望:如果你的題目與我的完全一樣!!!就不要完全照抄,對你對我都不好!!!函數功能都是完整的。。。

  1. 線性探測
    H(i)=(H(key)+di)%m (di = 1 2 3 4 - - - m-1.)
      從發生衝突的位置開始,依次繼續向後探測,直到有空位置。
    插入時:使用哈希函數找到待插入元素在哈希表中的位置,如果該位置沒有元素則直接插入,如果該位置有元素但不是待插入元素則發生哈希衝突,使用線性探測找到下一個空位置,在找空位置的路上如果遇到元素與待插入元素相同則不插入(即哈希表中不允許有相同的元素),沒有遇到等找到空位置就插入。
  2. .二次探測
      發生哈希衝突時,二次探測尋找下一個空位置的公式爲:H(i)=(H(key)+di)%m
    di = 1^2 , -1^2, 2^2 ,.-2^2, - - -(±)k^2.(K<=m/2),其中m都是表示表長。
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define max 14
#define casue 13
#define DataType int
typedef struct Hash{
	int findnum;
	DataType key;
}Hash;

void Createhash(Hash *hash,int kind)
{
	printf("How many data you want to input ? \n");
	int num,data;
	int j,count=0;
	scanf("%d",&num);
	while(num>max){
		printf("out of the hash list's max capacity !\n");
		printf("please input again !\n");
		scanf("%d",&num);
	};
	getchar();
	int p=1,q=1;
	for(int i=0;i<num;i++){
		printf("input the %d data\n",i+1);
		scanf("%d",&data);
		getchar();
		if(kind==1){//一次探測再散列
			do{
				j = (data+count)%casue;
				count++;
				if(hash[j].key == 0){
					hash[j].key=data;
					hash[j].findnum=count;
					count=0;
				}
			}while(count!=0);
		}else{//二次探測再散列解決衝突
			do{
				j=data%casue;
				if(count>0){//有衝突 
					if(count%2==1){
						j+=pow(q,2);
						q++;
					}else{
						j-=pow(p,2);
						p++;
					}
				}
				count++;
				if(j<0){
					j += max;
				}else if(j>max){
					j %= max;
				}
				else{
					if(hash[j].key == 0){
						hash[j].key=data;
						hash[j].findnum=count;
						count=0;
					}
				}
			}while(count!=0);
		}
	}	
}

void insert(Hash *hash,int data,int insert,int count){
	hash[insert].key = data;
	hash[insert].findnum = count;
}

int findkey(Hash *hash , int k,int kind)
{
	int j,i=0;
	bool find = true;
	if(kind == 1){
		do{
			j = (k+i)%casue;
			if(hash[j].key == k){
				find = false;
			}else if(hash[j].key == 0){
				find = false;////沒有找到
				insert(hash,k,j,i+1);//插入
				j=-1;
			}
			i++;
			if(i>max){
				printf("hash list is full\n");
				exit(0);
			}
		}while(find);
	}else{
		int p=1,q=1,count=0;
		do{
			j=k%casue;
			//奇偶判斷 
			if(count>0){//有衝突 
				if(count%2==1){
					j+=pow(q,2);
					q++;
				}else{
					j-=pow(p,2);
					p++;
				}
			}
			count++;
			if(j<0){
				j+=max;
			}else if(j>max){
					j %= max;
			}else{
				if(hash[j].key == k){
					find = false;
				}else if(hash[j].key == 0){
					find = false;//沒有找到
					insert(hash,k,j,count);
					j=-1;//查找不成功 沒有該元素
				}
			}
			if(count>max){
				printf("hash list is full\n");
				exit(0);
			}
		}while(find);
	}
	return j;
}

int main()
{
	Hash hash[max];
	int kind;
	printf("1.一次探測再散列 2.二次探測再散列解決衝突\n");
	scanf("%d",&kind);
	Createhash(hash,kind);
	printf("which key do you want find ?\n"); 
	scanf("%d",&k);
	getchar();
	int i = findkey(hash,k,kind);//i=-1則就是沒有該元素這裏的判斷被我刪了
	printf("address: %d data: %d findnum: %d \n",i,hash[i].key,hash[i].findnum);
	return 0;
}

這裏我刪除了一些代碼我怕有人直接照着我的抄那就不好。如果你看懂了就不能增加了,因爲我只在主函數刪了一些。其他的函數還是很完整的。

在這裏插入圖片描述

最後再輸入一個數據你想查找的數據。

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章