size
Lektion 7 von 13 im Kurs Stack - Datenstrukturen-Serie #1 von Coddy.
Aufgabe
EinfachFüge zu Stack eine Methode namens size hinzu, die keine Argumente entgegennimmt und die Anzahl der aktuell im Stack befindlichen Elemente zurückgibt.
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);
}
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;
}
}
return 0;
}