Menu
Coddy logo textTech

pop

Coddyの「スタック - データ構造シリーズ #1」コースのレッスン 6/13。

challenge icon

チャレンジ

簡単

Stackpop という名前のメソッドを追加してください。このメソッドは引数を受け取らず、スタックの最上部にある整数を削除、その値を返します。 

自分で試してみよう

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

スタック - データ構造シリーズ #1のすべてのレッスン