Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- ArrayList<Integer> stack = new ArrayList<>(10000);
- int size = 0;
- String command;
- while (true) {
- command = sc.nextLine();
- if (command.contains("push")) {
- stack.add(Integer.parseInt(command.substring(5)));
- size++;
- System.out.println("ok");
- }
- if (command.contains("pop")) {
- if (size <= 0) {
- System.out.println("error");
- } else {
- System.out.println(stack.remove(size - 1));
- size--;
- }
- }
- if (command.contains("back")) {
- if (size <= 0) {
- System.out.println("error");
- } else {
- System.out.println(stack.get(size - 1));
- }
- }
- if (command.contains("size")) {
- System.out.println(size);
- }
- if (command.contains("clear")) {
- if (size == 0) {
- System.out.println("ok");
- } else {
- stack.clear();
- size = 0;
- System.out.println("ok");
- }
- }
- if (command.contains("exit")) {
- System.out.println("bye");
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment