C语言实现哈希表

哈希表

1、哈希表的创建

#define MAX 10
#define NULL_KEY -1
typedef int data_type;
typedef struct{
    data_type *ele;
    int n;

}hash_table;

hash_table *create_hash_table(){
hash_table * ht=(hash_table *)malloc(sizeof(hash_table));
ht->n=0;
ht->ele=(data_type *)malloc(sizeof(data_type)*MAX);
for(int i=0;i<MAX;i++){
    ht->ele[i]=NULL_KEY;
}
return ht;
}

2、判满

int is_full(hash_table *ht){
    return ht->n==MAX?1:0;
}

3、使用开放地址法解决冲突

void insert_hash_table(hash_table *ht,data_type key){
    if(is_full(ht)){
    printf("hash is full\n");
        return;
    }
    int index=key%MAX;
    while(ht->ele[index]!=NULL_KEY){
        index=(index+1)%MAX;
    }
    ht->ele[index]=key;
    ht->n++;
    return;
}

4、查找key

int search_hash_key(hash_table *ht,data_type key){
int index=key%MAX;
while(ht->ele[index]!=key){
    index=(index+1)%10;
    //找了一圈没找到,或者找到-1了
    if(index==key%MAX||ht->ele[index]==NULL_KEY){
        return -1;
    }
}
return index;

}

5、遍历哈希表

void print_hash(hash_table *ht){
    for(int i=0;i<MAX;i++){
        printf("%d ",ht->ele[i]);
    }
    return;
}

6、使用链地址法

#define MAX 7
typedef int data_type;

typedef struct node{
     struct node *next;
     data_type data;
}hash_node;
//二重指针是用来存放指针的地址
//使用指针数组(二重指针)保存
hash_node **create_hash_table(){
    hash_node **ht=(hash_node **)malloc(sizeof(hash_node *)*MAX);
    memset(ht,0,sizeof(hash_node *)*MAX);
    for(int i=0;i<MAX;i++){
        ht[i]=NULL;
    }
    return ht;
}
//插入数据
void insert_hash_data(hash_node **h,data_type key){
    int index=key%MAX;
    hash_node **p=NULL;
    hash_node *temp=NULL;
 

    for(p=&h[index];*p !=NULL;p=&((*p)->next)){
      if((*p)->data>key){
        break;
        }
    }
    temp=(hash_node *)malloc(sizeof(hash_node));
    temp->data=key;
    //*p是前一个节点的next里面存放的地址
    temp->next=*p;
    *p=temp;
   return;

}

void printf_hash_table(hash_node **h){
    int i=0;
    hash_node **p=NULL;
    for(int i=0;i<MAX;i++){
        printf("index=%d :",i);
        for(p=&h[i];*p!=NULL;p=&((*p)->next)){
            printf("%d ",(*p)->data);
        }
        putchar('\n');
    }
    return;
}
  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是一种利用哈函数进行快速查找的数据结构。C语言可以通过数组和指针实现。 首先需要定义一个哈函数,将关键字转换成哈的位置。可以使用简单的取模运算,将关键字的值除以哈大小取余数得到哈位置。 例如,哈大小为10,关键字为20,则哈位置为20%10=2。 接下来,定义一个结构体来示哈中的每个元素,包括关键字和值。 ```c struct hash_element { int key; int value; }; ``` 然后,定义一个哈数组,将每个元素插入到哈中。如果哈位置已经被占用,可以使用链来解决冲突。 ```c #define HASH_SIZE 10 struct hash_element *hash_table[HASH_SIZE]; void insert(int key, int value) { int index = key % HASH_SIZE; struct hash_element *element = malloc(sizeof(struct hash_element)); element->key = key; element->value = value; if (hash_table[index] == NULL) { hash_table[index] = element; } else { struct hash_element *p = hash_table[index]; while (p->next != NULL) { p = p->next; } p->next = element; } } int search(int key) { int index = key % HASH_SIZE; struct hash_element *p = hash_table[index]; while (p != NULL) { if (p->key == key) { return p->value; } p = p->next; } return -1; } ``` 这里的insert函数将关键字和值封装成一个结构体,然后根据哈函数计算出哈位置。如果该位置为空,直接插入元素;否则,遍历链直到找到空位置插入。 search函数根据哈函数计算出哈位置,然后遍历链查找关键字。 以上是一个简单的哈实现,可以根据实际需求进行改进。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值