Advertisement
vov44k

Untitled

Nov 20th, 2022
916
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.08 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5.     static PrintWriter out;
  6.  
  7.     public static class Dequeue {
  8.         static int H, T;
  9.         static int[] arr;
  10.  
  11.         public Dequeue() {
  12.             H = 26000;
  13.             T = 26000;
  14.             arr = new int[100];
  15.         }
  16.  
  17.         void pushFront(int n) {
  18.             arr[T % 100] = n;
  19.             T++;
  20.             out.println("ok");
  21.         }
  22.  
  23.         void pushBack(int n) {
  24.             H--;
  25.             arr[H % 100] = n;
  26.             out.println("ok");
  27.         }
  28.  
  29.         static void popFront() {
  30.             if (T - H == 0) {
  31.                 out.println("error");
  32.                 return;
  33.             }
  34.             T--;
  35.             out.println(arr[T % 100]);
  36.         }
  37.  
  38.         static void popBack() {
  39.             if (T - H == 0) {
  40.                 out.println("error");
  41.                 return;
  42.             }
  43.             int n = arr[H % 100];
  44.             H++;
  45.             out.println(n);
  46.         }
  47.  
  48.         static void front() {
  49.             if (T - H == 0) {
  50.                 out.println("error");
  51.                 return;
  52.             }
  53.             out.println(arr[(T - 1) % 100]);
  54.         }
  55.  
  56.         static void back() {
  57.             if (T - H == 0) {
  58.                 out.println("error");
  59.                 return;
  60.             }
  61.             out.println(arr[H % 100]);
  62.         }
  63.  
  64.         static void size() {
  65.             out.println(Math.abs(T - H));
  66.         }
  67.  
  68.         static void clear() {
  69.             arr = new int[100];
  70.             H = 26000;
  71.             T = 26000;
  72.             out.println("ok");
  73.         }
  74.  
  75.         static void exit() {
  76.             out.println("bye");
  77.         }
  78.     }
  79.  
  80.     public static void main(String[] args) throws FileNotFoundException {
  81.         Scanner in = new Scanner(new File("input.txt"));
  82.         out = new PrintWriter("output.txt");
  83.  
  84.         Dequeue d = new Dequeue();
  85.  
  86.         while (in.hasNext()) {
  87.             String m = in.next();
  88.             if (m.equals("push_front")) {
  89.                 d.pushFront(in.nextInt());
  90.                 continue;
  91.             }
  92.             if (m.equals("push_back")) {
  93.                 d.pushBack(in.nextInt());
  94.                 continue;
  95.             }
  96.             if (m.equals("pop_front")) {
  97.                 d.popFront();
  98.                 continue;
  99.             }
  100.             if (m.equals("pop_back")) {
  101.                 d.popBack();
  102.                 continue;
  103.             }
  104.             if (m.equals("front")) {
  105.                 d.front();
  106.                 continue;
  107.             }
  108.             if (m.equals("back")) {
  109.                 d.back();
  110.                 continue;
  111.             }
  112.             if (m.equals("size")) {
  113.                 d.size();
  114.                 continue;
  115.             }
  116.             if (m.equals("clear")) {
  117.                 d.clear();
  118.                 continue;
  119.             }
  120.             if (m.equals("exit")) {
  121.                 d.exit();
  122.                 in.close();
  123.                 out.close();
  124.                 return;
  125.             }
  126.         }
  127.         in.close();
  128.         out.close();
  129.     }
  130.  
  131. }
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement