Menu
CoddyTech

HashMap 类

Coddy 哈希表:数据结构系列第 4 讲 课程的第 3 课,共 14 课。

challenge icon

挑战

简单

编写一个类 HashMap,并添加一个不接收输入的构造函数。

在构造函数中,初始化三个字段:

  • 将 capacity 设置为 10。
  • 将 buckets 设置为一个包含 10 个空列表的数组(每个桶对应一个列表,用于链式处理)。
  • 将 count 设置为 0。

亲手试试

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hashmap.h"

int main() {
    HashMap h;
    HashMap_init(&h);
    char line[256];
    while (fgets(line, sizeof(line), stdin)) {
        line[strcspn(line, "\r\n")] = '\0';
        char* cmd = strtok(line, " \t");
        if (!cmd) continue;
        if (strcmp(cmd, "state") == 0) { printf("%d %d %d\n", h.capacity, h.capacity, h.count); }
    }
    return 0;
}

哈希表:数据结构系列第 4 讲中的所有课

自己练习: C 在线编译器