isEmpty
Lição 9 de 14 do curso Heaps e Filas de Prioridade - Série de Estruturas de Dados #7 da Coddy.
Desafio
FácilAdicione um método isEmpty à classe MinHeap.
Ele não recebe entrada e retorna:
truese o heap estiver vazio.falsese o heap tiver qualquer valor.
Experimente você mesmo
#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;
}