Menu
Coddy logo textTech

top

Coddy의 스택 - 자료구조 시리즈 #1 코스 레슨 — 13개 중 5번째.

challenge icon

챌린지

쉬움

Stack에 인수를 받지 않고 스택의 맨 위에 있는 정수를 반환하는 top이라는 메서드를 추가하세요. 

직접 해보기

#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;
        }
    }
    return 0;
}

스택 - 자료구조 시리즈 #1의 모든 레슨