chillurbrain

7. Простой стек.

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