Menu
Coddy logo textTech

size

Lektion 8 von 14 im Kurs Heaps & Priority Queues - Datenstrukturen-Serie #7 von Coddy.

challenge icon

Aufgabe

Einfach

Füge der Klasse MinHeap eine Methode size hinzu.

Sie nimmt keine Eingabe entgegen und gibt die aktuelle Anzahl der Werte im Heap zurück.

Probier es selbst

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

int main() {
    MinHeap h;
    MinHeap_init(&h);
    char line[1024];
    while (fgets(line, sizeof(line), stdin)) {
        line[strcspn(line, "\r\n")] = '\0';
        char* cmd = strtok(line, " \t");
        if (!cmd) continue;
        if (strcmp(cmd, "insert") == 0) { char* arg = strtok(NULL, " \t"); if (arg) MinHeap_insert(&h, atoi(arg)); }
        if (strcmp(cmd, "peek") == 0) { printf("%d\n", MinHeap_peek(&h)); }
        if (strcmp(cmd, "extractMin") == 0) { printf("%d\n", MinHeap_extractMin(&h)); }
        if (strcmp(cmd, "size") == 0) { printf("%d\n", MinHeap_size(&h)); }
    }
    return 0;
}

Alle Lektionen in Heaps & Priority Queues - Datenstrukturen-Serie #7

Übe selbstständig: Online-C-Compiler