散列——实验及提升训练

第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);
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值