hash
Coddy의 Hash Table - 자료구조 시리즈 #4 코스 레슨 — 14개 중 4번째.
챌린지
쉬움HashMap 클래스에 hash 메서드를 추가하세요.
이 메서드는 정수형 key를 인자로 받아 key % capacity로 계산된 해당 키의 버킷 인덱스를 반환합니다.
직접 해보기
#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); }
if (strcmp(cmd, "hash") == 0) { char* arg = strtok(NULL, " \t"); if (arg) printf("%d\n", HashMap_hash(&h, atoi(arg))); }
}
return 0;
}