isEmpty
Lektion 9 von 14 im Kurs Heaps & Priority Queues - Datenstrukturen-Serie #7 von Coddy.
Aufgabe
EinfachFügen Sie der Klasse MinHeap eine Methode isEmpty hinzu.
Sie nimmt keine Eingabe entgegen und gibt Folgendes zurück:
true, wenn der Heap leer ist.false, wenn der Heap Werte enthält.
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)); }
if (strcmp(cmd, "isEmpty") == 0) { printf("%s\n", MinHeap_isEmpty(&h) ? "true" : "false"); }
}
return 0;
}