chillurbrain

10.1.14 - 10.1.16. Очередь.

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