Menu
Coddy logo textTech

push

Lektion 4 von 13 im Kurs Stack - Datenstrukturen-Serie #1 von Coddy.

challenge icon

Aufgabe

Einfach

Füge der Klasse Stack eine Methode namens push hinzu, die eine Ganzzahl entgegennimmt und diese oben auf den Stack legt. 

Probier es selbst

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

Alle Lektionen in Stack - Datenstrukturen-Serie #1