Стек с защитой от ошибок
include <iostream>
include <deque>
include <string>
using namespace std;
int main() { deque<int> dq; string c; while (cin >> c) { if (c == "push") {int n; cin >> n; dq.push_back(n); cout << "ok\n";} else if (c == "pop") {cout << (dq.empty() ? "error\n" : to_string(dq.back()) + "\n"); if (!dq.empty()) dq.pop_back();} else if (c == "back") cout << (dq.empty() ? "error\n" : to_string(dq.back()) + "\n"); else if (c == "size") cout << dq.size() << "\n"; else if (c == "clear") {dq.clear(); cout << "ok\n";} else if (c == "exit") {cout << "bye\n"; break;} } return 0; }
import java.util.ArrayList; import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Integer> s = new ArrayList<Integer>(); int c = 0;
while(in.hasNext()) {
String cmd = in.next();
if("push".equals(cmd)) {
s.add(in.nextInt()); c++;
System.out.println("ok");
}
else
if("pop".equals(cmd)) {
if(c == 0) System.out.println("error");
else System.out.println(s.remove(--c));
}
else
if("back".equals(cmd)) {
if(c == 0) System.out.println("error");
else System.out.println(s.get(c-1));
}
else
if("size".equals(cmd)) System.out.println(c);
else
if("clear".equals(cmd)) {
s.clear(); c = 0;
System.out.println("ok");
}
else { System.out.println("bye"); break; }
}
} }