empty
Lección 8 de 13 del curso Pila - Serie de Estructuras de Datos #1 de Coddy.
Desafío
FácilAñade a Stack un método llamado empty que no reciba argumentos y devuelva true si la pila está vacía, de lo contrario false.
Pruébalo tú mismo
#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;
}
Todas las lecciones de Pila - Serie de Estructuras de Datos #1
Practica por tu cuenta: Compilador de C online