empty
Coddy의 스택 - 자료구조 시리즈 #1 코스 레슨 — 13개 중 8번째.
챌린지
쉬움Stack에 인수를 받지 않고 스택이 비어 있으면 true를, 그렇지 않으면 false를 반환하는 empty라는 메서드를 추가하세요.
직접 해보기
#include <iostream>
#include <sstream>
#include <string>
#include "stack.h"
int main() {
Stack stack;
std::string line;
while (std::getline(std::cin, line)) {
std::istringstream iss(line);
std::string cmd;
if (!(iss >> cmd)) continue;
if (cmd == "push") {
int x; iss >> x; stack.push(x);
}
else if (cmd == "top") {
std::cout << stack.top() << std::endl;
}
else if (cmd == "pop") {
std::cout << stack.pop() << std::endl;
}
else if (cmd == "size") {
std::cout << stack.size() << std::endl;
}
else if (cmd == "empty") {
std::cout << (stack.empty() ? "true" : "false") << std::endl;
}
}
return 0;
}