Advertisement
vov44k

58

Nov 27th, 2022
774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.87 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5.     public static void main(String[] args) throws FileNotFoundException {
  6.         Scanner in = new Scanner(new File("input.txt"));
  7.         PrintWriter pw = new PrintWriter("output.txt");
  8.  
  9.         My_Queue queue = new My_Queue();
  10.         String a;
  11.         do {
  12.             a = in.next();
  13.  
  14.             switch (a) {
  15.                 case "push":
  16.                     queue.push(in.nextInt());
  17.                     pw.println("ok");
  18.                     break;
  19.                 case "pop":
  20.                     if (queue.size() == 0) pw.println("error");
  21.                     else pw.println(queue.pop());
  22.                     break;
  23.  
  24.                 case "front":
  25.                     if (queue.size() == 0) pw.println("error");
  26.                     else pw.println(queue.peek());
  27.                     break;
  28.  
  29.                 case "size":
  30.                     pw.println(queue.size());
  31.                     break;
  32.                 case "clear":
  33.                     queue.clear();
  34.                     pw.println("ok");
  35.                     break;
  36.             }
  37.         } while (!a.equals("exit"));
  38.  
  39.         pw.println("bye");
  40.  
  41.  
  42.         in.close();
  43.         pw.close();
  44.  
  45.  
  46.     }
  47.  
  48.  
  49.     static class My_Queue {
  50.         private int[] array;
  51.         private int top;
  52.         private int bottom;
  53.  
  54.         public My_Queue() {
  55.             array = new int[1000000];
  56.             top = 0;
  57.             bottom=0;
  58.         }
  59.  
  60.  
  61.         public void push(int q) {
  62.             array[top++] = q;
  63.         }
  64.  
  65.         public int size() {
  66.             return top-bottom;
  67.         }
  68.  
  69.         public int pop() {
  70.             return array[bottom++];
  71.         }
  72.  
  73.         public int peek() {
  74.             return array[bottom];
  75.         }
  76.  
  77.         public void clear() {
  78.             top = 0;
  79.             bottom = 0;
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement