入栈
Coddy 栈:数据结构系列 #1 课程的第 4 课,共 13 课。
挑战
简单为 Stack 添加一个名为 push 的方法,该方法接收一个整数,并将它推入栈顶。
亲手试试
#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;
}
栈:数据结构系列 #1中的所有课
自己练习: C 在线编译器