第1关:哈希表初始化
#include <stdio.h>
#include <stdlib.h>
/*哈希结构*/
struct hashTable
{
int *element;//存放键值的连续空间起始地址
int maxNum;// 哈希表长度
int curNum;//当前哈希表已有数据元素
};
struct node
{
int data ;
struct node *next;
};
struct hashTable_link
{
struct node *element;
int maxNum;// 哈希表长度
};
/*第一关*/
//初始化一个哈希表,能满足线性探测再散列处理冲突法使用,初始化哈希表元素均为-1,表示该位置为空,可插入
struct hashTable* initHashTable_linear()
{
struct hashTable *s=(struct hashTable*)malloc(sizeof(struct hashTable));
if(s!=NULL)
{
s->maxNum=10;
s->curNum=7;
s->element=(int*)malloc(sizeof(int)*s->maxNum);
for(int i=0;i<s->maxNum;i++)
{
s->element[i]=-1;
}
return s;
}
return NULL;
}
//初始化一个哈希表,能满足拉链法处理冲突法使用。初始化哈希表元素为0,用于计算该条链中数据个数,在插入时增加
struct hashTable_link * initHashTable_link()
{
struct hashTable_link *s=(struct hashTable_link*)malloc(sizeof(struct hashTable_link));
if(s!=NULL)
{
s->maxNum=10;
s->element=(struct node*)malloc(sizeof(struct node)*s->maxNum);
for(int i=0;i<s->maxNum;i++)
{
s->element[i].data=0;
}
return s;
}
return NULL;
}
/*第二关*/
//输出线性探测再散列法构建的哈希表,从下标为0的元素开始输出,每输出一个数据空一格
void printHashTable(struct hashTable *h)
{
for(int i=0;i<h->maxNum;i++)
{
printf("%d ",h->element[i]);
}
}
/*第三关*/
//哈希函数,h(key) = (key*3) % 7
int hashFun(int key)
{
int f;
f=(key*3)%7;
return f;
}
//函数功能:计算key的哈希地址,若发生冲突,则使用线性探测再散列的方法查找合适的插入位置下标,并返回该下标
int findPos(struct hashTable *h , int key)
{
int f,t;
for(int i=0;i<h->maxNum;i++)
{
f=hashFun(key);
if(i==f&&h->element[i]==-1)
{
h->element[i]=key;
}
else if(i==f&&h->element[i]!=-1)
{
for(t=i+1;t<h->maxNum;t++)
{
if(h->element[t]==-1)//第一个key
{
h->element[t]=key;
return t;
}
}
for(int j=0;j<i;j++)
{
if(h->element[j]==-1)//第一个key
{
h->element[j]=key;
return j;
}
}
}
}
}
//插入键值函数,若哈希表空间已满,则返回-1,否则返回插入位置下标
int insertKey(struct hashTable *h , int key)
{
if(h->curNum==h->maxNum)
{
return -1;
}
else
{
return findPos(h,key);
}
}
第2关:输出哈希表
#include <stdio.h>
#include <stdlib.h>
/*哈希结构*/
struct hashTable
{
int *element;//存放键值的连续空间起始地址
int maxNum;// 哈希表长度
int curNum;//当前哈希表已有数据元素
};
struct node
{
int data ;
struct node *next;
};
struct hashTable_link
{
struct node *element;
int maxNum;// 哈希表长度
};
/*第一关*/
//初始化一个哈希表,能满足线性探测再散列处理冲突法使用,初始化哈希表元素均为-1,表示该位置为空,可插入
struct hashTable* initHashTable_linear()
{
struct hashTable *s=(struct hashTable*)malloc(sizeof(struct hashTable));
if(s!=NULL)
{
s->maxNum=10;
s->curNum=7;
s->element=(int*)malloc(sizeof(int)*s->maxNum);
for(int i=0;i<s->maxNum;i++)
{
s->element[i]=-1;
}
return s;
}
return NULL;
}
//初始化一个哈希表,能满足拉链法处理冲突法使用。初始化哈希表元素为0,用于计算该条链中数据个数,在插入时增加
struct hashTable_link * initHashTable_link()
{
struct hashTable_link *s=(struct hashTable_link*)malloc(sizeof(struct hashTable_link));
if(s!=NULL)
{
s->maxNum=10;
s->element=(struct node*)malloc(sizeof(struct node)*s->maxNum);
for(int i=0;i<s->maxNum;i++)
{
s->element[i].data=0;
}
return s;
}
return NULL;
}
/*第二关*/
//输出线性探测再散列法构建的哈希表,从下标为0的元素开始输出,每输出一个数据空一格
void printHashTable(struct hashTable *h)
{
for(int i=0;i<h->maxNum;i++)
{
printf("%d ",h->element[i]);
}
}
/*第三关*/
//哈希函数,h(key) = (key*3) % 7
int hashFun(int key)
{
int f;
f=(key*3)%7;
return f;
}
//函数功能:计算key的哈希地址,若发生冲突,则使用线性探测再散列的方法查找合适的插入位置下标,并返回该下标
int findPos(struct hashTable *h , int key)
{
int f,t;
for(int i=0;i<h->maxNum;i++)
{
f=hashFun(key);
if(i==f&&h->element[i]==-1)
{
h->element[i]=key;
}
else if(i==f&&h->element[i]!=-1)
{
for(t=i+1;t<h->maxNum;t++)
{
if(h->element[t]==-1)//第一个key
{
h->element[t]=key;
return t;
}
}
for(int j=0;j<i;j++)
{
if(h->element[j]==-1)//第一个key
{
h->element[j]=key;
return j;
}
}
}
}
}
//插入键值函数,若哈希表空间已满,则返回-1,否则返回插入位置下标
int insertKey(struct hashTable *h , int key)
{
if(h->curNum==h->maxNum)
{
return -1;
}
else
{
return findPos(h,key);
}
}
第3关:插入键值(线性探测再散列法处理冲突)
#include <stdio.h>
#include <stdlib.h>
/*哈希结构*/
struct hashTable
{
int *element;//存放键值的连续空间起始地址
int maxNum;// 哈希表长度
int curNum;//当前哈希表已有数据元素
};
struct node
{
int data ;
struct node *next;
};
struct hashTable_link
{
struct node *element;
int maxNum;// 哈希表长度
};
/*第一关*/
//初始化一个哈希表,能满足线性探测再散列处理冲突法使用,初始化哈希表元素均为-1,表示该位置为空,可插入
struct hashTable* initHashTable_linear()
{
struct hashTable *s=(struct hashTable*)malloc(sizeof(struct hashTable));
if(s!=NULL)
{
s->maxNum=10;
s->curNum=7;
s->element=(int*)malloc(sizeof(int)*s->maxNum);
for(int i=0;i<s->maxNum;i++)
{
s->element[i]=-1;
}
return s;
}
return NULL;
}
//初始化一个哈希表,能满足拉链法处理冲突法使用。初始化哈希表元素为0,用于计算该条链中数据个数,在插入时增加
struct hashTable_link * initHashTable_link()
{
struct hashTable_link *s=(struct hashTable_link*)malloc(sizeof(struct hashTable_link));
if(s!=NULL)
{
s->maxNum=10;
s->element=(struct node*)malloc(sizeof(struct node)*s->maxNum);
for(int i=0;i<s->maxNum;i++)
{
s->element[i].data=0;
}
return s;
}
return NULL;
}
/*第二关*/
//输出线性探测再散列法构建的哈希表,从下标为0的元素开始输出,每输出一个数据空一格
void printHashTable(struct hashTable *h)
{
for(int i=0;i<h->maxNum;i++)
{
printf("%d ",h->element[i]);
}
}
/*第三关*/
//哈希函数,h(key) = (key*3) % 7
int hashFun(int key)
{
int f;
f=(key*3)%7;
return f;
}
//函数功能:计算key的哈希地址,若发生冲突,则使用线性探测再散列的方法查找合适的插入位置下标,并返回该下标
int findPos(struct hashTable *h , int key)
{
int f,t;
for(int i=0;i<h->maxNum;i++)
{
f=hashFun(key);
if(i==f&&h->element[i]==-1)
{
h->element[i]=key;
}
else if(i==f&&h->element[i]!=-1)
{
for(t=i+1;t<h->maxNum;t++)
{
if(h->element[t]==-1)//第一个key
{
h->element[t]=key;
return t;
}
}
for(int j=0;j<i;j++)
{
if(h->element[j]==-1)//第一个key
{
h->element[j]=key;
return j;
}
}
}
}
}
//插入键值函数,若哈希表空间已满,则返回-1,否则返回插入位置下标
int insertKey(struct hashTable *h , int key)
{
if(h->curNum==h->maxNum)
{
return -1;
}
else
{
return findPos(h,key);
}
}