Menu
Coddy logo textTech

HashMap Class

Lesson 3 of 14 in Coddy's Hash Tables - Data Structures Series #4 course.

challenge icon

Challenge

Easy

Write a class HashMap with a constructor that takes no input.

In the constructor, initialize three fields:

  • capacity set to 10.
  • buckets set to an array of 10 empty lists (one per bucket, for chaining).
  • count set to 0.

Try it yourself

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

All lessons in Hash Tables - Data Structures Series #4