empty
Lesson 8 of 13 in Coddy's Stack - Data Structures Series #1 course.
Challenge
EasyAdd to Stack a method called empty that gets no arguments and returns true if the stack is empty, otherwise false.
Try it yourself
#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;
}
All lessons in Stack - Data Structures Series #1
Practice on your own: Online C compiler