Menu
CoddyTech

vuoto

Lezione 8 di 13 del corso Pila - Serie sulle strutture dati #1 di Coddy.

challenge icon

Sfida

Facile

Aggiungi a Stack un metodo chiamato empty che non accetta argomenti e restituisce true se lo stack è vuoto, altrimenti false.

Provalo tu

#include <stdio.h>
#include <string.h>
#include "stack.h"

int main() {
    Stack stack;
    Stack_init(&stack);
    char cmd[16];
    while (scanf("%15s", cmd) == 1) {
        if (strcmp(cmd, "push") == 0) {
            int x;
            scanf("%d", &x);
            Stack_push(&stack, x);
        }
        else if (strcmp(cmd, "top") == 0) {
            printf("%d\n", Stack_top(&stack));
        }
        else if (strcmp(cmd, "pop") == 0) {
            printf("%d\n", Stack_pop(&stack));
        }
        else if (strcmp(cmd, "size") == 0) {
            printf("%d\n", Stack_size(&stack));
        }
        else if (strcmp(cmd, "empty") == 0) {
            printf("%s\n", Stack_empty(&stack) ? "true" : "false");
        }
    }
    return 0;
}

Tutte le lezioni di Pila - Serie sulle strutture dati #1

Esercitati da solo: Compilatore C online