chillurbrain

10.1.17. Дек.

May 26th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 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.         LinkedList<Integer> deque = new LinkedList<>();
  8.         int size = 0;
  9.         String command;
  10.  
  11.         while (true) {
  12.             command = sc.nextLine();
  13.             if (command.contains("push_front")) {
  14.                 deque.addFirst(Integer.parseInt(command.substring(11)));
  15.                 size++;
  16.                 System.out.println("ok");
  17.             }
  18.             if (command.contains("push_back")) {
  19.                 deque.addLast(Integer.parseInt(command.substring(10)));
  20.                 size++;
  21.                 System.out.println("ok");
  22.             }
  23.             if (command.equals("pop_front")) {
  24.                 if (size <= 0) {
  25.                     System.out.println("error");
  26.                 } else {
  27.                     System.out.println(deque.removeFirst());
  28.                     size--;
  29.                 }
  30.             }
  31.             if (command.equals("pop_back")) {
  32.                 if (size <= 0) {
  33.                     System.out.println("error");
  34.                 } else {
  35.                     System.out.println(deque.removeLast());
  36.                     size--;
  37.                 }
  38.             }
  39.             if (command.equals("front")) {
  40.                 if (size <= 0) {
  41.                     System.out.println("error");
  42.                 } else {
  43.                     System.out.println(deque.getFirst());
  44.                 }
  45.             }
  46.             if (command.equals("back")) {
  47.                 if (size <= 0) {
  48.                     System.out.println("error");
  49.                 } else {
  50.                     System.out.println(deque.getLast());
  51.                 }
  52.             }
  53.             if (command.equals("size")) {
  54.                 System.out.println(size);
  55.             }
  56.             if (command.equals("clear")) {
  57.                 if (size == 0) {
  58.                     System.out.println("ok");
  59.                 } else {
  60.                     for (int i = 0; i < size; i++) {
  61.                         deque.removeFirst();
  62.                     }
  63.                     size = 0;
  64.                     System.out.println("ok");
  65.                 }
  66.             }
  67.             if (command.contains("exit")) {
  68.                 System.out.println("bye");
  69.                 break;
  70.             }
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment