Advertisement
vov44k

t61

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