vuoto
Lezione 8 di 13 del corso Pila - Serie sulle strutture dati #1 di Coddy.
Sfida
FacileAggiungi a Stack un metodo chiamato empty che non accetta argomenti e restituisce true se lo stack è vuoto, altrimenti false.
Provalo tu
#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;
}
else if (cmd == "empty") {
std::cout << (stack.empty() ? "true" : "false") << std::endl;
}
}
return 0;
}
Tutte le lezioni di Pila - Serie sulle strutture dati #1
3Sfide di esercitazione
Esercitazione #1Esercitazione #2Esercitazione #3Esercitazione #4Esercitazione #5Esercitati da solo: Compilatore C online