Menu
Coddy logo textTech

push

Lesson 4 of 13 in Coddy's Stack - Data Structures Series #1 course.

challenge icon

Challenge

Easy

Add to Stack a method called push that gets an integer and pushes the integer to the top of the stack. 

Try it yourself

#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);
        }
    }
    for (int element : stack.elements) {
        std::cout << element << std::endl;
    }
    return 0;
}

All lessons in Stack - Data Structures Series #1