Class HashMap
Pelajaran 3 dari 14 dalam kursus Tabel Hash, Seri Struktur Data #4 di Coddy.
Tantangan
MudahTulis sebuah HashMap dengan constructor yang tidak menerima input.
Di dalam constructor, inisialisasi tiga field:
capacitydiatur ke 10.bucketsdiatur ke sebuah array yang berisi 10 list kosong (satu untuk setiap bucket, untuk chaining).countdiatur ke 0.
Coba sendiri
#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;
}
Semua pelajaran di Tabel Hash, Seri Struktur Data #4
Latihan sendiri: Compiler C online