פונקציית גיבוב
שיעור 4 מתוך 14 בקורס טבלאות גיבוב – סדרת מבני נתונים #4 של Coddy.
אתגר
קלהוסף מתודה hash למחלקה HashMap.
היא מקבלת מספר שלם 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;
}
כל השיעורים ביחידה טבלאות גיבוב – סדרת מבני נתונים #4
תרגלו בעצמכם: קומפיילר C אונליין