size
Coddy의 스택 - 자료구조 시리즈 #1 코스 레슨. 13개 중 7번째.
챌린지
쉬움Stack에 인수를 받지 않고 현재 스택에 있는 요소의 개수를 반환하는 size라는 메서드를 추가하세요.
직접 해보기
#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;
}
스택 - 자료구조 시리즈 #1의 모든 레슨
직접 연습해 보세요: 온라인 C 컴파일러