chillurbrain

9. Стек неограниченного размера

May 22nd, 2016
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.         ArrayList<Integer> stack = new ArrayList<>(10000);
  9.         int size = 0;
  10.         String command;
  11.  
  12.         while (true) {
  13.             command = sc.nextLine();
  14.             if (command.contains("push")) {
  15.                 stack.add(Integer.parseInt(command.substring(5)));
  16.                 size++;
  17.                 System.out.println("ok");
  18.             }
  19.             if (command.contains("pop")) {
  20.                 if (size <= 0) {
  21.                     System.out.println("error");
  22.                 } else {
  23.                     System.out.println(stack.remove(size - 1));
  24.                     size--;
  25.                 }
  26.             }
  27.             if (command.contains("back")) {
  28.                 if (size <= 0) {
  29.                     System.out.println("error");
  30.                 } else {
  31.                     System.out.println(stack.get(size - 1));
  32.                 }
  33.             }
  34.             if (command.contains("size")) {
  35.                 System.out.println(size);
  36.             }
  37.             if (command.contains("clear")) {
  38.                 if (size == 0) {
  39.                     System.out.println("ok");
  40.                 } else {
  41.                     stack.clear();
  42.                     size = 0;
  43.                     System.out.println("ok");
  44.                 }
  45.             }
  46.             if (command.contains("exit")) {
  47.                 System.out.println("bye");
  48.                 break;
  49.             }
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment