C语言数据结构学习笔记(25)-哈希表链地址法

# include <stdio.h>
# include <string.h>
# include <windows.h>
# define MAX_SIZE 10
# define HASH_SIZE 10
typedef struct person{
    char name[MAX_SIZE];
    int age;
}Person;
typedef struct node{
    Person * data;
    struct node * next;
}Node;
typedef struct hashtable{
    struct node * head;
    int hash_size;
    int count;
}HashTable;

int hashCode(Person * p){
    int len = strlen(p->name);
    int code = 0;
    if(NULL == p->name)
        code = 0;
    else{
        for(int i = 0; i < len; i++){
            char ch = p->name[i];
            code = (code << 5) - code + ch;
        }
    }
    return code;
}
int Hash(Person * p){
    return hashCode(p)% HASH_SIZE;
}
HashTable * CreateTable(){
    HashTable * H = (HashTable *)malloc(sizeof(HashTable));
    H->hash_size = HASH_SIZE;
    H->count = 0;
    H->head = (Node*)malloc(H->hash_size*(sizeof(Node)));
    for(int i = 0; i < H->hash_size; i++)
        H->head[i].next = NULL;
    return H;
}
Node * Search(HashTable * H, Person * p){
    int index = Hash(p);
    Node * q = H->head[index].next;
    while(q && strcmp(p->name, q->data->name))
        q = q->next;
    return q;
}
void Insert(HashTable * H, Person * p){
    Node * q = Search(H, p);
    if(q){
        q->data->age = p->age;
        strcpy(q->data->name, p->name);
    }
    else{
        Node * New = (Node*)malloc(sizeof(Node));
        New->data = (Person*)malloc(sizeof(Person));
        New->data->age = p->age;
        strcpy(New->data->name, p->name);
        New->next = NULL;
        int index = Hash(New->data);
        if(NULL == H->head[index].next){
            H->head[index].next = New;
        }
        else{
            Node * r = H->head[index].next;
            Node * s = r->next;
            while(s){
                r = s;
                s = s->next;
            }
            r->next = New;
        }
        /*
        New->next = H->head[index].next;
        H->head[index].next = New;
        */
        H->count++;
    }
}
void Print(HashTable * H){
    for(int i = 0; i < H->hash_size; i++){
        Node * p = H->head[i].next;
        if(!p)
            printf("NULL");
        while(p){
            printf("%s--%d ", p->data->name, p->data->age);
            p = p->next;
        }
        printf("\n");
    }
    printf("count = %d\n", H->count);
}
void Destory(HashTable ** H){
    for(int i = 0; i < (*H)->hash_size; i++){
        Node * p = (*H)->head[i].next;
        while(p){
            Node * q = p->next;
            free(p);
            p = q;
        }
    }
    free((*H)->head);
    (*H)->head = NULL;
    free(*H);
    *H = NULL;
}
int main(){
    HashTable * H = CreateTable();
    person p1 = {"jack", 18};
    person p2 = {"Lily", 19};
    person p3 = {"mike", 17};
    person p4 = {"laven", 20};
    person p5 = {"mike1", 22};
    Insert(H, &p1);
    Insert(H, &p2);
    Insert(H, &p3);
    Insert(H, &p4);
    Insert(H, &p5);
    Print(H);
    Destory(&H);
    Print(H);
    system("pause");
    return 0;
}
 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值