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

相关推荐

  1. C语言实现

    2024-04-29 11:28:03       10 阅读
  2. 手动数字-C语言

    2024-04-29 11:28:03       16 阅读
  3. c

    2024-04-29 11:28:03       14 阅读
  4. c语言数据结构之

    2024-04-29 11:28:03       13 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-29 11:28:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-29 11:28:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-29 11:28:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-29 11:28:03       18 阅读

热门阅读

  1. Stable Diffusion Windows部署教程

    2024-04-29 11:28:03       15 阅读
  2. Linux连接不上Android设备

    2024-04-29 11:28:03       13 阅读
  3. 安卓手机APP开发__媒体开发部分__用户界面定制

    2024-04-29 11:28:03       12 阅读
  4. android:maxEms=“5“ 为什么可以显示6个文字呢?

    2024-04-29 11:28:03       6 阅读
  5. SpringMVC

    SpringMVC

    2024-04-29 11:28:03      11 阅读
  6. Android 11在app中修改屏幕亮度

    2024-04-29 11:28:03       12 阅读
  7. [SQL系列]从零开始学Clickhouse

    2024-04-29 11:28:03       12 阅读
  8. Docker-05 Docker容器命令

    2024-04-29 11:28:03       8 阅读
  9. C#三人飞行棋

    2024-04-29 11:28:03       9 阅读
  10. 用 Python 进行渗透测试

    2024-04-29 11:28:03       11 阅读
  11. 【K8s】工作以来遇到的K8s相关问题、故障

    2024-04-29 11:28:03       6 阅读
  12. 加密,解密 crypto-js、 计算哈希值,js-sha3

    2024-04-29 11:28:03       9 阅读