首页 > 其他分享 >哈希集合、哈希表的拉链法实现

哈希集合、哈希表的拉链法实现

时间:2023-12-28 18:44:47浏览次数:33  
标签:head obj struct 拉链 int 哈希 next key 集合

哈希表

// 拉链法
struct ListNode {
    int val;
    struct ListNode *next;
};

typedef struct {
    struct ListNode *data;
} MyHashSet;

// 模
const int hashSize = 1009;

MyHashSet *myHashSetCreate() {
    MyHashSet *myHashSet = (MyHashSet *) malloc(sizeof(MyHashSet));
    myHashSet->data = (struct ListNode *) malloc(sizeof(struct ListNode) * (hashSize + 1));
    for (int i = 0; i <= hashSize; ++i) {
        myHashSet->data[i].val = -1;
        myHashSet->data[i].next = NULL;
    }
    return myHashSet;
}

// 散列
int hash(int key) {
    return key % hashSize;
}

struct ListNode *getList(MyHashSet *obj, int key) {
    return &(obj->data[hash(key)]);
}

// 返回查找的节点
struct ListNode *findNode(struct ListNode *head, int key) {
    struct ListNode *p = head;
    while (p != NULL && p->next != NULL) {
        if (p->next->val == key) return p;
        p = p->next;
    }
    return NULL;
}

bool containsNode(struct ListNode *head, int key) {
    return findNode(head, key) != NULL;
}

void insertNode(struct ListNode *head, int key) {
    struct ListNode *node = (struct ListNode *) malloc(sizeof(struct ListNode));
    node->val = key;
    node->next = head->next;
    head->next = node;
}

void removeNode(struct ListNode *head, int key) {
    struct ListNode *p = head;
    while (p != NULL && p->next != NULL) {
        // 如果存在也只会有一个节点
        if (p->next->val == key){
            p->next = p->next->next;
            return;
        } 
        p = p->next;
    }
}

void myHashSetAdd(MyHashSet *obj, int key) {
    struct ListNode *head = getList(obj, key);
    if (containsNode(head, key)) return;
    insertNode(head, key);
}

void myHashSetRemove(MyHashSet *obj, int key) {
    struct ListNode *head = getList(obj, key);
    if (!containsNode(head, key)) return;
    removeNode(head, key);
}

bool myHashSetContains(MyHashSet *obj, int key) {
    struct ListNode *head = getList(obj, key);
    return containsNode(head, key);
}

void myHashSetFree(MyHashSet *obj) {
    if (obj != NULL) {
        if (obj->data != NULL) {
            free(obj->data);
            obj->data = NULL;
        }
        free(obj);
        obj = NULL;
    }
}
// 拉链法
struct MyListNode {
    int key;
    int value;
    struct MyListNode *next;
};

typedef struct {
    struct MyListNode *data;
} MyHashMap;

const int hashSize = 1009;

MyHashMap *myHashMapCreate() {
    MyHashMap *myHashMap = (MyHashMap *) malloc(sizeof(MyHashMap));
    myHashMap->data = (struct MyListNode *) malloc(sizeof(struct MyListNode) * (hashSize + 1));
    for (int i = 0; i <= hashSize; ++i) {
        myHashMap->data[i].key = -1;
        myHashMap->data[i].next = NULL;
    }
    return myHashMap;
}

int hash(int key) {
    return key % hashSize;
}

struct MyListNode *getList(MyHashMap *obj, int key) {
    return &(obj->data[hash(key)]);
}

struct MyListNode *findNode(struct MyListNode *head, int key) {
    while (head != NULL) {
        if (head->key == key)return head;
        head = head->next;
    }
    return NULL;
}

void insertNode(struct MyListNode *head, int key, int value) {
    struct MyListNode *node = (struct MyListNode *) malloc(sizeof(struct MyListNode));
    node->key = key;
    node->value = value;
    node->next = head->next;
    head->next = node;
}

void removeNode(struct MyListNode *head, int key) {
    while (head != NULL && head->next != NULL) {
        if (head->next->key == key) {
            head->next = head->next->next;
            return;
        }
        head = head->next;
    }
}

void myHashMapPut(MyHashMap *obj, int key, int value) {
    struct MyListNode *head = getList(obj, key);
    struct MyListNode *node = findNode(head, key);
    if (node == NULL) {
        insertNode(head, key, value);
    } else {
        node->value = value;
    }
}

int myHashMapGet(MyHashMap *obj, int key) {
    struct MyListNode *head = getList(obj, key);
    struct MyListNode *node = findNode(head, key);
    if (node != NULL) return node->value;
    return -1;
}

void myHashMapRemove(MyHashMap *obj, int key) {
    struct MyListNode *head = getList(obj, key);
    struct MyListNode *node = findNode(head, key);
    if (node != NULL) removeNode(head, key);
}

void myHashMapFree(MyHashMap *obj) {
    if (obj != NULL) {
        if (obj->data != NULL) {
            free(obj->data);
            obj->data = NULL;
        }
        free(obj);
        obj = NULL;
    }
}

标签:head,obj,struct,拉链,int,哈希,next,key,集合
From: https://www.cnblogs.com/sprinining/p/17933328.html

相关文章

  • Java的集合
    一.Java集合框架概述    一方面,面向对象语言对事物的体现都是以对象的形式,为了方便对多个对象的操作,就要对对象进行存储。另一方面,使用Array存储对象方面具有一些弊端,而Java集合就像一种容器,可以动态的把多个对象的引用放入容器中。1.数组Array存储(1)数组在内存存储方面的特......
  • 24-集合(主要介绍ArrayList)
    ArrayList长度可变的原理1)当创建ArrayList集合容器的时候,底层会存在一个长度为10哥大小的空数组2)当容器的大小不满足时,创建(扩容)原数组1.5倍大小的新数组3)将原数组数据,拷贝到新数组中4)将新元素添加到新数组 ArrayList集合的构造方法1)publicArrayList():创建一个空的集......
  • 【HMS Core】推送问题小集合
    ​【问题描述1】“一个应用订阅的主题数量不能超过2000个”,如果超过了,会出现什么情况,如何解决?【解决方案】主题数量上限是2000,超过后会导致订阅主题失败。可以尝试删除不需要的主题。https://developer.huawei.com/consumer/cn/doc/HMSCore-References/topic-delete-api-00000......
  • C#深度理解:数组、集合、哈希、字典、堆、栈 优缺点
    一、数组1、Array固定数组优点:1).快速访问:数组通过索引来访问元素,访问速度非常快,因为可以通过索引进行直接定位。2).内存连续存储:数组在内存中以连续的方式存储元素,这样有助于提高数据的读取和写入效率。3).多维支持:C#中的数组支持多维(二维、三维等)数据结构,可以用于表示......
  • 关于集合的扩展 C#
    ///<summary>//////</summary>///<typeparamname="T"></typeparam>///<paramname="sources"></param>///<paramname="details"></param&......
  • 关于密码哈希算法BCrypt的编码结果各部分意义分析及其他注意事项
    找到一个英文的解析:Thebcryptstandardmakesstoringsaltseasy-everythingitneedstocheckapasswordisstoredintheoutputstring.Theprefix"$2a$"or"2y"inahashstringinashadowpasswordfileindicatesthathashstringisabcr......
  • 儿童笑话集合
    动物故事1、在草原上,有两只牛在吃草.其中一头牛对另一头说:"最近流行疯牛病,你说我们会不会得?"另一头牛答道:"我们怕什么,我们不是袋鼠么?""呃~~~你~~疯~~~了?~~~"2、两头牛还在一起吃草,一头牛问另一头:“喂!你的草是什么味道?”牛答道:“草莓味!”那牛高兴地也过来吃一口,愤怒地喊到:“你骗我!”......
  • 12.15数学学习笔记——1.1集合的概念
    把研究对象统称为元素,把一些元素组成的总体叫做集合。给定一个集合,那么一个元素在或者不在这个集合中就确定了。一个给定集合中的元素是互不相同的(集合中的元素是不重复出现的)。只要构成两个集合的元素是一样的,我们就称这两个集合是相等的。如果说a是集合A的元素,就说a属于集合......
  • [转][译] 密码哈希的方法:PBKDF2,Scrypt,Bcrypt 和 ARGON2
    原文地址:PasswordHashing:PBKDF2,Scrypt,BcryptandARGON2原文作者:MichelePreziuso译文出自:掘金翻译计划本文永久链接:https://github.com/xitu/gold-miner/blob/master/TODO1/password-hashing-pbkdf2-scrypt-bcrypt-and-argon2.md译者:司徒公子校对者:xionglong58、GJX......
  • java集合stream操作
    forEach-遍历Stream<Integer>stream=Stream.of(2,3,1,4);stream.forEach(System.out::println);filter-过滤Stream<Integer>stream=Stream.of(2,3,1,4);Stream<Integer>newStream=stream.filter(num->num>2);System.out.pr......