数据结构复习7, 简单的实现一下哈希表

//
//  Hash_Table.c
//  Data_structure
//
//  Created by 양송 on 2020/06/06.
//  Copyright © 2020 양송. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
    char* name;
    struct node* next;
}Node;

typedef struct
{
    Node* Head_Node;
}Linked_List;

typedef struct
{
    int Hash_size;
    Linked_List* Hash_Table;
}Hash;

int return_index(char name)
{
    return name-'A';
}

Hash* Hash_Table_Init()
{
    Hash* H = (Hash*)malloc(sizeof(Hash));
    H->Hash_size = 26;
    H->Hash_Table = (Linked_List*)malloc(sizeof(Linked_List)*26);//一共有26个字母
    for(int i = 0; i< H->Hash_size;i++)
    {
        H->Hash_Table[i].Head_Node = (Node*)malloc(sizeof(Node));
        H->Hash_Table[i].Head_Node->name = NULL;
        H->Hash_Table[i].Head_Node->next = NULL;
    }
    return H;
}

void Hash_insert(Hash** H, char* name)
{
    if(H)
    {
        int index = return_index(name[0]);//查看首字母的
        Node* temp = (*H)->Hash_Table[index].Head_Node;
        Node* new_node = (Node*)malloc(sizeof(Node));
        new_node->name = name;
        new_node->next = NULL;
        temp->next = new_node;
    }
}
Node* Hash_find(Hash* H, char* name)
{
    if(H)
    {
        int index = return_index(name[0]);
        if(H->Hash_Table[index].Head_Node->next != NULL)
        {
            Node* temp = H->Hash_Table[index].Head_Node;
            while(temp)
            {
                if(temp->name == name)
                    return temp;
                else
                    temp = temp->next;
            }
        }
        else
            return NULL;
    }
    return NULL;
}

void Hash_Data_remove(Hash** H, char* name)
{
    if(H)
    {
        int index = return_index(name[0]);
        if((*H)->Hash_Table[index].Head_Node->next != NULL)
        {
            Node* temp = (*H)->Hash_Table[index].Head_Node;
            Node* finder = (*H)->Hash_Table[index].Head_Node->next;
            while(finder)
            {
                if(finder->name == name)
                {
                    temp->next = finder->next;
                    return;
                }
                finder = finder->next;
                temp = temp->next;
            }
            printf("not found\n");
            
        }
    }
}

void print_Hash_Table(Hash *H)
{
    if(H)
    {
        
        for(int i = 0; i<H->Hash_size;i++)
        {
            Node* temp = H->Hash_Table[i].Head_Node->next;
            printf("Hash_Table[%d]:",i);
            while(temp)
            {
                printf(" [%s]",temp->name);
                temp = temp->next;
                printf("->");
            }
            printf(" NULL\n");
        }
        printf("\n");
    }
}

int main()
{
    Hash* H = NULL;
    H = Hash_Table_Init();
    
    Hash_insert(&H, "ZhangSan");
    Hash_insert(&H, "LiSi");
    Hash_insert(&H, "WangEr");
    
    print_Hash_Table(H);
    
    Hash_Data_remove(&H, "LiSi");
    print_Hash_Table(H);
    
   
    
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值