push
Lesson 4 of 13 in Coddy's Stack - Data Structures Series #1 course.
Challenge
EasyAdd to Stack a method called push that gets an integer and pushes the integer to the top of the stack.
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);
}
}
for (int i = 0; i < stack.size; i++) {
printf("%d\n", stack.elements[i]);
}
return 0;
}
All lessons in Stack - Data Structures Series #1
Practice on your own: Online C compiler