size
Lección 7 de 13 del curso Pila - Serie de Estructuras de Datos #1 de Coddy.
Desafío
FácilAñade a Stack un método llamado size que no reciba argumentos y devuelva el número de elementos actualmente en la pila.
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));
}
}
return 0;
}
Todas las lecciones de Pila - Serie de Estructuras de Datos #1
Practica por tu cuenta: Compilador de C online